feat: improve OpenAI client
- Updated the OpenAI client to use environment variables for API keys. - Improved the configuration and management of OpenAI clients. - Added retry mechanism to HTTP requests for improved reliability. Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
|
||||
!!hero_code.generate_client
|
||||
name: "openai"
|
||||
classname: "OpenAI"
|
||||
hasconfig: false
|
||||
singleton: false
|
||||
default: true
|
||||
title: ""
|
||||
name:'openai'
|
||||
classname:'OpenAI'
|
||||
singleton:0
|
||||
default:1
|
||||
hasconfig:1
|
||||
reset:0
|
||||
@@ -2,7 +2,6 @@ module openai
|
||||
|
||||
import freeflowuniverse.herolib.core.base
|
||||
import freeflowuniverse.herolib.core.playbook
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
|
||||
__global (
|
||||
openai_global map[string]&OpenAI
|
||||
@@ -14,35 +13,93 @@ __global (
|
||||
@[params]
|
||||
pub struct ArgsGet {
|
||||
pub mut:
|
||||
name string
|
||||
name string = 'default'
|
||||
}
|
||||
|
||||
fn args_get(args_ ArgsGet) ArgsGet {
|
||||
mut model := args_
|
||||
if model.name == '' {
|
||||
model.name = openai_default
|
||||
mut args := args_
|
||||
if args.name == '' {
|
||||
args.name = openai_default
|
||||
}
|
||||
if model.name == '' {
|
||||
model.name = 'default'
|
||||
if args.name == '' {
|
||||
args.name = 'default'
|
||||
}
|
||||
return model
|
||||
return args
|
||||
}
|
||||
|
||||
pub fn get(args_ ArgsGet) !&OpenAI {
|
||||
mut args := args_get(args_)
|
||||
if args.name !in openai_global {
|
||||
if args.name == 'default' {
|
||||
if !config_exists(args) {
|
||||
if default {
|
||||
mut context := base.context() or { panic('bug') }
|
||||
context.hero_config_set('openai', model.name, heroscript_default()!)!
|
||||
}
|
||||
if !config_exists() {
|
||||
if default {
|
||||
config_save()!
|
||||
}
|
||||
load(args)!
|
||||
}
|
||||
config_load()!
|
||||
}
|
||||
return openai_global[args.name] or {
|
||||
println(openai_global)
|
||||
panic('could not get config for ${args.name} with name:${model.name}')
|
||||
panic('bug in get from factory: ')
|
||||
}
|
||||
}
|
||||
|
||||
fn config_exists(args_ ArgsGet) bool {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context() or { panic('bug') }
|
||||
return context.hero_config_exists('openai', args.name)
|
||||
}
|
||||
|
||||
fn config_load(args_ ArgsGet) ! {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context()!
|
||||
mut heroscript := context.hero_config_get('openai', args.name)!
|
||||
play(heroscript: heroscript)!
|
||||
}
|
||||
|
||||
fn config_save(args_ ArgsGet) ! {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context()!
|
||||
context.hero_config_set('openai', args.name, heroscript_default()!)!
|
||||
}
|
||||
|
||||
fn set(o OpenAI) ! {
|
||||
mut o2 := obj_init(o)!
|
||||
openai_global['default'] = &o2
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct PlayArgs {
|
||||
pub mut:
|
||||
name string = 'default'
|
||||
heroscript string // if filled in then plbook will be made out of it
|
||||
plbook ?playbook.PlayBook
|
||||
reset bool
|
||||
|
||||
start bool
|
||||
stop bool
|
||||
restart bool
|
||||
delete bool
|
||||
configure bool // make sure there is at least one installed
|
||||
}
|
||||
|
||||
pub fn play(args_ PlayArgs) ! {
|
||||
mut args := args_
|
||||
|
||||
if args.heroscript == '' {
|
||||
args.heroscript = heroscript_default()!
|
||||
}
|
||||
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
|
||||
|
||||
mut install_actions := plbook.find(filter: 'openai.configure')!
|
||||
if install_actions.len > 0 {
|
||||
for install_action in install_actions {
|
||||
mut p := install_action.params
|
||||
cfg_play(p)!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// switch instance to be used for openai
|
||||
pub fn switch(name string) {
|
||||
openai_default = name
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ module openai
|
||||
|
||||
import freeflowuniverse.herolib.data.paramsparser
|
||||
import freeflowuniverse.herolib.core.httpconnection
|
||||
import os
|
||||
|
||||
pub const version = '1.14.3'
|
||||
const singleton = false
|
||||
@@ -12,7 +13,7 @@ pub fn heroscript_default() !string {
|
||||
heroscript := "
|
||||
!!openai.configure
|
||||
name:'openai'
|
||||
key: 'YOUR_API_KEY'
|
||||
api_key: ${os.getenv('OPENAI_API_KEY')}
|
||||
"
|
||||
|
||||
return heroscript
|
||||
@@ -49,6 +50,7 @@ pub fn (mut client OpenAI) connection() !&httpconnection.HTTPConnection {
|
||||
name: 'openaiconnection_${client.name}'
|
||||
url: 'https://api.openai.com/v1'
|
||||
cache: false
|
||||
retry: 20
|
||||
)!
|
||||
c2
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user