From d86dac8ab6b08453656e17668614cfb12dac5079 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 10:24:51 -0500 Subject: [PATCH 1/6] fix(openai): completion subscriptions --- .grit/patterns/python/openai.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index 01e1ada9..d1c96d47 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -562,3 +562,26 @@ response = client.chat.completions.create( ] ) ``` + +## Fix subscripting + +The new API does not support subscripting on the outputs. + +```python +import openai + +model, token_limit, prompt_cost, comp_cost = 'gpt-4-32k', 32_768, 0.06, 0.12 + +completion = openai.ChatCompletion.create( + model=model, + messages=[ + {"role": "system", "content": system}, + {"role": "user", "content": + user + text}, + ] +) +output = completion['choices'][0]['message']['content'] + +prom = completion['usage']['prompt_tokens'] +comp = completion['usage']['completion_tokens'] +``` From def73539669d313895dcd526f2e2d1c0cb3d52f0 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 12:20:26 -0500 Subject: [PATCH 2/6] output --- .grit/patterns/python/openai.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index d1c96d47..a616c95d 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -585,3 +585,25 @@ output = completion['choices'][0]['message']['content'] prom = completion['usage']['prompt_tokens'] comp = completion['usage']['completion_tokens'] ``` + +```python +from openai import OpenAI + +client = OpenAI() + +model, token_limit, prompt_cost, comp_cost = 'gpt-4-32k', 32_768, 0.06, 0.12 + +completion = client.chat.completions.create( + model=model, + messages=[ + {"role": "system", "content": system}, + {"role": "user", "content": + user + text}, + ] +) + +output = completion.choices[0].message.content + +prom = completion.usage.prompt_tokens +comp = completion.usage.completion_tokens +``` From 74792739e78db6d4410fba3f9416cb348756c56b Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 12:52:02 -0500 Subject: [PATCH 3/6] fix it --- .grit/patterns/python/openai.md | 49 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index a616c95d..92e6463c 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -198,6 +198,21 @@ pattern pytest_patch() { }, } + + +// When there is a variable used by an openai call, make sure it isn't subscripted +pattern fix_downstream_openai_usage() { + $var where { + $program <: maybe contains bubble or { + `$var['choices'][$n]` => `$var.choices[$n]`, + `$var['choices'][$n]['$x']` => `$var.choices[$n].$x`, + `$var['choices'][$n]['$x']['$y']` => `$var.choices[$n].$.$y`, + `$var['$x']['$y']` => `$var.$x.$y`, + `$var['$x']` => `$var.$x`, + } + } +} + pattern openai_main($client, $azure) { $body where { if ($client <: undefined) { @@ -257,6 +272,9 @@ pattern openai_main($client, $azure) { contains `import openai` as $import_stmt where { $body <: contains bubble($has_sync, $has_async, $has_openai_import, $body, $client, $azure) `openai.$res.$func($params)` as $stmt where { $res <: rewrite_whole_fn_call(import = $has_openai_import, $has_sync, $has_async, $res, $func, $params, $stmt, $body, $client, $azure), + $stmt <: maybe within bubble($stmt) `$var = $stmt` where { + $var <: fix_downstream_openai_usage() + } }, }, contains `from openai import $resources` as $partial_import_stmt where { @@ -548,19 +566,15 @@ response = openai.ChatCompletion.create( import os from openai import AzureOpenAI -client = AzureOpenAI( - azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), - api_key=os.getenv("AZURE_OPENAI_KEY"), - api_version="2023-05-15" -) +client = AzureOpenAI(azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), +api_key=os.getenv("AZURE_OPENAI_KEY"), +api_version="2023-05-15") -response = client.chat.completions.create( - model="gpt-35-turbo", - messages=[ +response = client.chat.completions.create(model="gpt-35-turbo", +messages=[ {"role": "system", "content": "You are a helpful assistant."}, - ] -) +]) ``` ## Fix subscripting @@ -593,15 +607,12 @@ client = OpenAI() model, token_limit, prompt_cost, comp_cost = 'gpt-4-32k', 32_768, 0.06, 0.12 -completion = client.chat.completions.create( - model=model, - messages=[ - {"role": "system", "content": system}, - {"role": "user", "content": - user + text}, - ] -) - +completion = client.chat.completions.create(model=model, +messages=[ + {"role": "system", "content": system}, + {"role": "user", "content": + user + text}, +]) output = completion.choices[0].message.content prom = completion.usage.prompt_tokens From 98bcadc7f22fa4076576a91825fa3a551fe02afa Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 12:56:10 -0500 Subject: [PATCH 4/6] format and test --- .grit/patterns/python/openai.md | 16 ++++++++++------ .grit/patterns/python/openai_azure.md | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index 92e6463c..d9b59bfb 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -566,15 +566,19 @@ response = openai.ChatCompletion.create( import os from openai import AzureOpenAI -client = AzureOpenAI(azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), -api_key=os.getenv("AZURE_OPENAI_KEY"), -api_version="2023-05-15") +client = AzureOpenAI( + azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), + api_key=os.getenv("AZURE_OPENAI_KEY"), + api_version="2023-05-15" +) -response = client.chat.completions.create(model="gpt-35-turbo", -messages=[ +response = client.chat.completions.create( + model="gpt-35-turbo", + messages=[ {"role": "system", "content": "You are a helpful assistant."}, -]) + ] +) ``` ## Fix subscripting diff --git a/.grit/patterns/python/openai_azure.md b/.grit/patterns/python/openai_azure.md index 65a1d22b..1856f2e7 100644 --- a/.grit/patterns/python/openai_azure.md +++ b/.grit/patterns/python/openai_azure.md @@ -64,7 +64,7 @@ response = client.chat.completions.create( ] ) -print(response['choices'][0]['message']['content']) +print(response.choices[0].message.content) ``` ## Embeddings @@ -99,7 +99,7 @@ response = client.embeddings.create( input="Your text string goes here", model="YOUR_DEPLOYMENT_NAME" ) -embeddings = response['data'][0]['embedding'] +embeddings = response.data[0].embedding print(embeddings) ``` From 03b0141fcdbab086744d5dc280c78bafa229bc17 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 14:25:24 -0500 Subject: [PATCH 5/6] counter-example --- .grit/patterns/python/openai.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index d9b59bfb..f91477da 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -602,6 +602,9 @@ output = completion['choices'][0]['message']['content'] prom = completion['usage']['prompt_tokens'] comp = completion['usage']['completion_tokens'] + +# unrelated variable +foo = something['else'] ``` ```python @@ -621,4 +624,7 @@ output = completion.choices[0].message.content prom = completion.usage.prompt_tokens comp = completion.usage.completion_tokens + +# unrelated variable +foo = something['else'] ``` From 2b5acf02f65ed6700048608e581915c2c9c3d0e4 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Wed, 10 Jan 2024 14:26:38 -0500 Subject: [PATCH 6/6] much better --- .grit/patterns/python/openai.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.grit/patterns/python/openai.md b/.grit/patterns/python/openai.md index f91477da..a19f056e 100644 --- a/.grit/patterns/python/openai.md +++ b/.grit/patterns/python/openai.md @@ -203,12 +203,8 @@ pattern pytest_patch() { // When there is a variable used by an openai call, make sure it isn't subscripted pattern fix_downstream_openai_usage() { $var where { - $program <: maybe contains bubble or { - `$var['choices'][$n]` => `$var.choices[$n]`, - `$var['choices'][$n]['$x']` => `$var.choices[$n].$x`, - `$var['choices'][$n]['$x']['$y']` => `$var.choices[$n].$.$y`, - `$var['$x']['$y']` => `$var.$x.$y`, - `$var['$x']` => `$var.$x`, + $program <: maybe contains bubble($var) `$x['$y']` as $sub => `$x.$y` where { + $sub <: contains $var } } }