Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 65 additions & 40 deletions .grit/patterns/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ pattern deprecated_resource_cls() {
}


pattern rename_func($has_sync, $has_async, $res, $stmt, $params) {
pattern rename_func($has_sync, $has_async, $res, $stmt, $params, $client) {
$func where {
if ($func <: r"a([a-zA-Z0-9]+)"($func_rest)) {
$has_async = `true`,
$func => $func_rest,
$stmt => `aclient.$res.$func($params)`,
if ($client <: undefined) {
$stmt => `aclient.$res.$func($params)`,
} else {
$stmt => `$client.$res.$func($params)`,
}
} else {
$has_sync = `true`,
$stmt => `client.$res.$func($params)`,
if ($client <: undefined) {
$stmt => `client.$res.$func($params)`,
} else {
$stmt => `$client.$res.$func($params)`,
}
},
// Fix function renames
if ($res <: `Image`) {
Expand Down Expand Up @@ -107,11 +115,11 @@ pattern change_import($has_sync, $has_async, $need_openai_import) {
}
}

pattern rewrite_whole_fn_call($import, $has_sync, $has_async, $res, $func, $params, $stmt, $body) {
pattern rewrite_whole_fn_call($import, $has_sync, $has_async, $res, $func, $params, $stmt, $body, $client) {
or {
rename_resource() where {
$import = `true`,
$func <: rename_func($has_sync, $has_async, $res, $stmt, $params),
$func <: rename_func($has_sync, $has_async, $res, $stmt, $params, $client),
},
deprecated_resource() as $dep_res where {
$stmt_whole = $stmt,
Expand Down Expand Up @@ -178,48 +186,65 @@ pattern pytest_patch() {
},
}

file($body) where {
$need_openai_import = `false`,
$has_openai_import = `false`,
$has_partial_import = `false`,
$has_sync = `false`,
$has_async = `false`,

// Remap errors
$body <: maybe contains `openai.error.$exp` => `openai.$exp` where {
$need_openai_import = `true`,
},
pattern openai_main($client) {
$body where {
if ($client <: undefined) {
$need_openai_import = `false`,
$create_client = true,
} else {
$need_openai_import = `true`,
$create_client = false,
},
$has_openai_import = `false`,
$has_partial_import = `false`,
$has_sync = `false`,
$has_async = `false`,

// Remap errors
$body <: maybe contains `openai.error.$exp` => `openai.$exp` where {
$need_openai_import = `true`,
},

// Mark all the places where we they configure openai as something that requires manual intervention
$body <: maybe contains bubble($need_openai_import) `openai.$field = $val` => `raise Exception("The 'openai.$field' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI($field=$val)'")` where {
$need_openai_import = `true`,
},
if ($client <: undefined) {
// Mark all the places where we they configure openai as something that requires manual intervention
$body <: maybe contains bubble($need_openai_import) `openai.$field = $val` => `raise Exception("The 'openai.$field' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI($field=$val)'")` where {
$need_openai_import = `true`,
},
},

$body <: maybe contains `import openai` as $import_stmt where {
$body <: contains bubble($has_sync, $has_async, $has_openai_import, $body) `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),
$body <: maybe contains `import openai` as $import_stmt where {
$body <: contains bubble($has_sync, $has_async, $has_openai_import, $body, $client) `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),
},
},
},

$body <: maybe contains `from openai import $resources` as $partial_import_stmt where {
$has_partial_import = `true`,
$body <: contains bubble($has_sync, $has_async, $resources) `$res.$func($params)` as $stmt where {
$resources <: contains $res,
$res <: rewrite_whole_fn_call($import, $has_sync, $has_async, $res, $func, $params, $stmt, $body),
}
},
$body <: maybe contains `from openai import $resources` as $partial_import_stmt where {
$has_partial_import = `true`,
$body <: contains bubble($has_sync, $has_async, $resources, $client) `$res.$func($params)` as $stmt where {
$resources <: contains $res,
$res <: rewrite_whole_fn_call($import, $has_sync, $has_async, $res, $func, $params, $stmt, $body, $client),
}
},

if ($has_openai_import <: `true`) {
$import_stmt <: change_import($has_sync, $has_async, $need_openai_import),
if ($has_partial_import <: `true`) {
$partial_import_stmt => .,
if ($create_client <: true) {
if ($has_openai_import <: `true`) {
$import_stmt <: change_import($has_sync, $has_async, $need_openai_import),
if ($has_partial_import <: `true`) {
$partial_import_stmt => .,
},
} else if ($has_partial_import <: `true`) {
$partial_import_stmt <: change_import($has_sync, $has_async, $need_openai_import),
},
},
} else if ($has_partial_import <: `true`) {
$partial_import_stmt <: change_import($has_sync, $has_async, $need_openai_import),
},

$body <: maybe contains unittest_patch(),
$body <: maybe contains pytest_patch(),
$body <: maybe contains unittest_patch(),
$body <: maybe contains pytest_patch(),
}
}

file($body) where {
// No client means instantiate one per file
$body <: openai_main()
}
```

Expand Down
74 changes: 74 additions & 0 deletions .grit/patterns/openai_global.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: Upgrade to OpenAI Python SDK - Global Client
---

Convert OpenAI from openai version to the v1 version, while continuing to use the global client. This is a variant of the [client-based version](https://github.com/getgrit/python/blob/main/.grit/patterns/openai.md).

tags: #python, #openai, #migration

```grit
engine marzano(0.1)
language python

file($body) where {
$body <: openai_main(client=`openai`)
}
```

## Rewrite completions

```python
import openai

completion = await openai.Completion.acreate(model="davinci-002", prompt="Hello world")
chat_completion = await openai.ChatCompletion.acreate(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
```

```python
import openai

completion = await openai.completions.create(model="davinci-002", prompt="Hello world")
chat_completion = await openai.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
```

## Global settings

If you use the global client, options can be set on itself.

```python
import openai

if openai_proxy:
openai.proxy = openai_proxy
openai.api_base = self.openai_api_base
```

```python
import openai

if openai_proxy:
openai.proxy = openai_proxy
openai.api_base = self.openai_api_base
```

## Remap errors

```python
import openai

try:
completion = openai.Completion.create(model="davinci-002", prompt="Hello world")
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
except openai.error.RateLimitError as err:
pass
```

```python
import openai

try:
completion = openai.completions.create(model="davinci-002", prompt="Hello world")
chat_completion = openai.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
except openai.RateLimitError as err:
pass
```