@@ -198,6 +198,17 @@ pattern pytest_patch() {
198
198
},
199
199
}
200
200
201
+
202
+
203
+ // When there is a variable used by an openai call, make sure it isn't subscripted
204
+ pattern fix_downstream_openai_usage() {
205
+ $var where {
206
+ $program <: maybe contains bubble($var) `$x['$y']` as $sub => `$x.$y` where {
207
+ $sub <: contains $var
208
+ }
209
+ }
210
+ }
211
+
201
212
pattern openai_main($client, $azure) {
202
213
$body where {
203
214
if ($client <: undefined) {
@@ -257,6 +268,9 @@ pattern openai_main($client, $azure) {
257
268
contains `import openai` as $import_stmt where {
258
269
$body <: contains bubble($has_sync, $has_async, $has_openai_import, $body, $client, $azure) `openai.$res.$func($params)` as $stmt where {
259
270
$res <: rewrite_whole_fn_call(import = $has_openai_import, $has_sync, $has_async, $res, $func, $params, $stmt, $body, $client, $azure),
271
+ $stmt <: maybe within bubble($stmt) `$var = $stmt` where {
272
+ $var <: fix_downstream_openai_usage()
273
+ }
260
274
},
261
275
},
262
276
contains `from openai import $resources` as $partial_import_stmt where {
@@ -562,3 +576,51 @@ response = client.chat.completions.create(
562
576
]
563
577
)
564
578
```
579
+
580
+ ## Fix subscripting
581
+
582
+ The new API does not support subscripting on the outputs.
583
+
584
+ ``` python
585
+ import openai
586
+
587
+ model, token_limit, prompt_cost, comp_cost = ' gpt-4-32k' , 32_768 , 0.06 , 0.12
588
+
589
+ completion = openai.ChatCompletion.create(
590
+ model = model,
591
+ messages = [
592
+ {" role" : " system" , " content" : system},
593
+ {" role" : " user" , " content" :
594
+ user + text},
595
+ ]
596
+ )
597
+ output = completion[' choices' ][0 ][' message' ][' content' ]
598
+
599
+ prom = completion[' usage' ][' prompt_tokens' ]
600
+ comp = completion[' usage' ][' completion_tokens' ]
601
+
602
+ # unrelated variable
603
+ foo = something[' else' ]
604
+ ```
605
+
606
+ ``` python
607
+ from openai import OpenAI
608
+
609
+ client = OpenAI()
610
+
611
+ model, token_limit, prompt_cost, comp_cost = ' gpt-4-32k' , 32_768 , 0.06 , 0.12
612
+
613
+ completion = client.chat.completions.create(model = model,
614
+ messages = [
615
+ {" role" : " system" , " content" : system},
616
+ {" role" : " user" , " content" :
617
+ user + text},
618
+ ])
619
+ output = completion.choices[0 ].message.content
620
+
621
+ prom = completion.usage.prompt_tokens
622
+ comp = completion.usage.completion_tokens
623
+
624
+ # unrelated variable
625
+ foo = something[' else' ]
626
+ ```
0 commit comments