Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.
Merged
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
87 changes: 43 additions & 44 deletions .grit/patterns/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,56 +216,58 @@ pattern openai_main($client, $azure) {
$has_async = `false`,

$client_params = [],
if ($client <: undefined) {

$body <: any {
// Mark all the places where we they configure openai as something that requires manual intervention
$body <: maybe contains bubble($need_openai_import, $azure, $client_params) `openai.$field = $val` as $setter where {
$field <: or {
`api_type` where {
$res = .,
if ($val <: or {`"azure"`, `"azure_ad"`}) {
$azure = true
if ($client <: undefined) {
contains bubble($need_openai_import, $azure, $client_params) `openai.$field = $val` as $setter where {
$field <: or {
`api_type` where {
$res = .,
if ($val <: or {`"azure"`, `"azure_ad"`}) {
$azure = true
},
},
},
`api_base` where {
$azure <: true,
$client_params += `azure_endpoint=$val`,
$res = .,
},
`api_key` where {
$res = .,
$client_params += `api_key=$val`,
},
`api_version` where {
$res = .,
// Only Azure has api_version
$azure = true,
$client_params += `api_version=$val`,
},
$_ where {
$res = todo(message=`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)'`, target=$setter),
$need_openai_import = `true`,
`api_base` where {
$azure <: true,
$client_params += `azure_endpoint=$val`,
$res = .,
},
`api_key` where {
$res = .,
$client_params += `api_key=$val`,
},
`api_version` where {
$res = .,
// Only Azure has api_version
$azure = true,
$client_params += `api_version=$val`,
},
$_ where {
$res = todo(message=`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)'`, target=$setter),
$need_openai_import = `true`,
}
}
}
} => $res
},

// Remap errors
$body <: maybe contains `openai.error.$exp` => `openai.$exp` where {
} => $res
},
// Remap errors
contains `openai.error.$exp` => `openai.$exp` 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, $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),
},
},

$body <: maybe contains `from openai import $resources` as $partial_import_stmt where {
},
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),
},
},
contains `from openai import $resources` as $partial_import_stmt where {
$has_partial_import = `true`,
$body <: contains bubble($has_sync, $has_async, $resources, $client, $azure) `$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, $azure),
}
},
contains unittest_patch(),
contains pytest_patch(),
},

if ($create_client <: true) {
Expand All @@ -278,9 +280,6 @@ pattern openai_main($client, $azure) {
$partial_import_stmt <: change_import($has_sync, $has_async, $need_openai_import, $azure, $client_params),
},
},

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

Expand Down