Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
github actions should work
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Dec 24, 2024
1 parent 60088e3 commit b8eb387
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 306 deletions.
3 changes: 0 additions & 3 deletions crystallib/clients/httpconnection/factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module httpconnection
import net.http
import freeflowuniverse.crystallib.clients.redisclient { RedisURL }


@[params]
pub struct HTTPConnectionArgs {
pub:
Expand Down Expand Up @@ -34,6 +33,4 @@ pub fn new(args HTTPConnectionArgs) !&HTTPConnection {
base_url: args.url.trim('/')
}
return &conn

}

8 changes: 4 additions & 4 deletions crystallib/clients/openai/embeddings.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub mut:
pub fn (mut f OpenAIClient[Config]) create_embeddings(args EmbeddingCreateArgs) !EmbeddingResponse {
mut conn := f.connection()!
return conn.post_json_generic[EmbeddingResponse](
method: .post
prefix: 'embeddings'
params: {
method: .post
prefix: 'embeddings'
params: {
'input': args.input.str()
'model': embedding_model_str(args.model)
'user': args.user
'user': args.user
}
dataformat: .json
)!
Expand Down
145 changes: 66 additions & 79 deletions crystallib/clients/openai/openai_factory_.v
Original file line number Diff line number Diff line change
@@ -1,118 +1,105 @@

module openai

import freeflowuniverse.crystallib.core.base
import freeflowuniverse.crystallib.core.playbook


__global (
openai_global map[string]&OpenAIClient
openai_default string
openai_global map[string]&OpenAIClient
openai_default string
)

/////////FACTORY

@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string = "default"
name string = 'default'
}

fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = openai_default
}
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = openai_default
}
if args.name == '' {
args.name = 'default'
}
return args
}

pub fn get(args_ ArgsGet) !&OpenAIClient {
mut args := args_get(args_)
if !(args.name in openai_global) {
if ! config_exists(){
if default{
config_save()!
}
}
config_load()!
}
return openai_global[args.name] or {
println(openai_global)
panic("bug in get from factory: ")
}
pub fn get(args_ ArgsGet) !&OpenAIClient {
mut args := args_get(args_)
if args.name !in openai_global {
if !config_exists() {
if default {
config_save()!
}
}
config_load()!
}
return openai_global[args.name] or {
println(openai_global)
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)
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)!
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()!)!
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_set('openai', args.name, heroscript_default()!)!
}


fn set(o OpenAIClient)! {
mut o2:=obj_init(o)!
openai_global["default"] = &o2
fn set(o OpenAIClient) ! {
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
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
mycfg:=cfg_play(p)!
set(mycfg)!
}
}

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
mycfg := cfg_play(p)!
set(mycfg)!
}
}
}




//switch instance to be used for openai
// switch instance to be used for openai
pub fn switch(name string) {
openai_default = name
openai_default = name
}
47 changes: 24 additions & 23 deletions crystallib/clients/openai/openai_model.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module openai

import freeflowuniverse.crystallib.data.paramsparser
import freeflowuniverse.crystallib.clients.httpconnection

Expand All @@ -7,41 +8,40 @@ const singleton = false
const default = true

pub fn heroscript_default() !string {
heroscript := "
heroscript := "
!!openai.configure
name:'openai'
openaikey:'your-api-key-here'
description:'OpenAI API Client'
url:'' //default is openai endpoint
"
return heroscript
return heroscript
}

@[heap]
pub struct OpenAIClient {
pub mut:
name string = 'default'
openaikey string @[secret]
description string
url string
conn ?&httpconnection.HTTPConnection
name string = 'default'
openaikey string @[secret]
description string
url string
conn ?&httpconnection.HTTPConnection
}

fn cfg_play(p paramsparser.Params) ! {
mut mycfg := OpenAIClient{
name: p.get_default('name', 'default')!
openaikey: p.get('openaikey')!
description: p.get_default('description', '')!
url: p.get_default('url', 'https://api.openai.com/v1')!
}
set(mycfg)!
}

fn obj_init(obj_ OpenAIClient)!OpenAIClient {
mut obj := obj_
return obj
mut mycfg := OpenAIClient{
name: p.get_default('name', 'default')!
openaikey: p.get('openaikey')!
description: p.get_default('description', '')!
url: p.get_default('url', 'https://api.openai.com/v1')!
}
set(mycfg)!
}

fn obj_init(obj_ OpenAIClient) !OpenAIClient {
mut obj := obj_
return obj
}

pub fn (mut client OpenAIClient) connection() !&httpconnection.HTTPConnection {
mut c := client.conn or {
Expand All @@ -53,8 +53,9 @@ pub fn (mut client OpenAIClient) connection() !&httpconnection.HTTPConnection {
)!
c2
}
c.default_header.set(.authorization, 'Bearer ${client.openaikey}')
// see https://modules.vlang.io/net.http.html#CommonHeader
client.conn = c
return c

c.default_header.set(.authorization, 'Bearer ${client.openaikey}')
// see https://modules.vlang.io/net.http.html#CommonHeader
client.conn = c
return c
}
4 changes: 2 additions & 2 deletions crystallib/clients/openrouter/models.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module openrouter

//https://openrouter.ai/docs/models
//todo: see also /root/code/github/freeflowuniverse/crystallib/crystallib/clients/openai/models.v
// https://openrouter.ai/docs/models
// todo: see also /root/code/github/freeflowuniverse/crystallib/crystallib/clients/openai/models.v
Loading

0 comments on commit b8eb387

Please sign in to comment.