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
|
!!hero_code.generate_client
|
||||||
name: "openai"
|
name:'openai'
|
||||||
classname: "OpenAI"
|
classname:'OpenAI'
|
||||||
hasconfig: false
|
singleton:0
|
||||||
singleton: false
|
default:1
|
||||||
default: true
|
hasconfig:1
|
||||||
title: ""
|
reset:0
|
||||||
@@ -2,7 +2,6 @@ module openai
|
|||||||
|
|
||||||
import freeflowuniverse.herolib.core.base
|
import freeflowuniverse.herolib.core.base
|
||||||
import freeflowuniverse.herolib.core.playbook
|
import freeflowuniverse.herolib.core.playbook
|
||||||
import freeflowuniverse.herolib.ui.console
|
|
||||||
|
|
||||||
__global (
|
__global (
|
||||||
openai_global map[string]&OpenAI
|
openai_global map[string]&OpenAI
|
||||||
@@ -14,35 +13,93 @@ __global (
|
|||||||
@[params]
|
@[params]
|
||||||
pub struct ArgsGet {
|
pub struct ArgsGet {
|
||||||
pub mut:
|
pub mut:
|
||||||
name string
|
name string = 'default'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args_get(args_ ArgsGet) ArgsGet {
|
fn args_get(args_ ArgsGet) ArgsGet {
|
||||||
mut model := args_
|
mut args := args_
|
||||||
if model.name == '' {
|
if args.name == '' {
|
||||||
model.name = openai_default
|
args.name = openai_default
|
||||||
}
|
}
|
||||||
if model.name == '' {
|
if args.name == '' {
|
||||||
model.name = 'default'
|
args.name = 'default'
|
||||||
}
|
}
|
||||||
return model
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(args_ ArgsGet) !&OpenAI {
|
pub fn get(args_ ArgsGet) !&OpenAI {
|
||||||
mut args := args_get(args_)
|
mut args := args_get(args_)
|
||||||
if args.name !in openai_global {
|
if args.name !in openai_global {
|
||||||
if args.name == 'default' {
|
if !config_exists() {
|
||||||
if !config_exists(args) {
|
if default {
|
||||||
if default {
|
config_save()!
|
||||||
mut context := base.context() or { panic('bug') }
|
|
||||||
context.hero_config_set('openai', model.name, heroscript_default()!)!
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
load(args)!
|
|
||||||
}
|
}
|
||||||
|
config_load()!
|
||||||
}
|
}
|
||||||
return openai_global[args.name] or {
|
return openai_global[args.name] or {
|
||||||
println(openai_global)
|
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.data.paramsparser
|
||||||
import freeflowuniverse.herolib.core.httpconnection
|
import freeflowuniverse.herolib.core.httpconnection
|
||||||
|
import os
|
||||||
|
|
||||||
pub const version = '1.14.3'
|
pub const version = '1.14.3'
|
||||||
const singleton = false
|
const singleton = false
|
||||||
@@ -12,7 +13,7 @@ pub fn heroscript_default() !string {
|
|||||||
heroscript := "
|
heroscript := "
|
||||||
!!openai.configure
|
!!openai.configure
|
||||||
name:'openai'
|
name:'openai'
|
||||||
key: 'YOUR_API_KEY'
|
api_key: ${os.getenv('OPENAI_API_KEY')}
|
||||||
"
|
"
|
||||||
|
|
||||||
return heroscript
|
return heroscript
|
||||||
@@ -49,6 +50,7 @@ pub fn (mut client OpenAI) connection() !&httpconnection.HTTPConnection {
|
|||||||
name: 'openaiconnection_${client.name}'
|
name: 'openaiconnection_${client.name}'
|
||||||
url: 'https://api.openai.com/v1'
|
url: 'https://api.openai.com/v1'
|
||||||
cache: false
|
cache: false
|
||||||
|
retry: 20
|
||||||
)!
|
)!
|
||||||
c2
|
c2
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user