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

Commit

Permalink
openai refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Dec 22, 2024
1 parent 4a830d5 commit 05f9ae7
Show file tree
Hide file tree
Showing 25 changed files with 465 additions and 309 deletions.
43 changes: 30 additions & 13 deletions crystallib/clients/canva/readme.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# canva
# Canva Module

> TODO: this module needs to be finished, just rough start
To get started

```vlang
This module provides a V client for interacting with the Canva API, enabling programmatic access to Canva's platform features.

## Setup

1. Create an account on [Canva Developer Portal](https://www.canva.com/developers/)
2. Create or select your application
3. Generate an API token
4. Configure the client using heroscript:
```v
import freeflowuniverse.crystallib.clients.canva
mut client:= canva.get()!
heroscript := "
!!canva.configure
name:'my_instance'
secret:'your-api-token-here'
"
client...
// Apply the configuration (only needs to be done once)
canva.play(heroscript: heroscript)!
```

## Usage

### Initialize Client
```v
// Get a configured client instance
mut cl := canva.get(name: 'my_instance')!
```

## example heroscript
### Examples

```hero
!!canva.configure
secret: '...'
```
#### Download a Design
```v
// Get client instance
mut cl := canva.get('my_instance')!
// Download a design by ID
design_result := cl.download('your-design-id')!
println('Design downloaded: ${design_result}')
```

52 changes: 0 additions & 52 deletions crystallib/clients/httpconnection/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,55 +117,3 @@ fn delete_ssh_key(mut conn HTTPConnection, fingerprint string) ! {
)!
}
```

## Custom Headers

You can set default headers for all requests or specify headers for individual requests:

```v
import net.http { Header }
// Set default headers for all requests
conn.default_header = http.new_header(
key: .authorization
value: 'Bearer your-token-here'
)
// Add custom headers for specific request
response := conn.get_json(
method: .get
prefix: 'protected/resource'
header: http.new_header(
key: .content_type
value: 'application/json'
)
)!
```

## Error Handling

The module uses V's built-in error handling. All methods that can fail return a Result type:

```v
// Handle potential errors
user := conn.get_json_generic[User](
method: .get
prefix: 'users/1'
) or {
println('Error: ${err}')
return
}
```

## Cache Configuration

The module supports caching of responses. Configure caching behavior through the `CacheConfig` struct:

```v
mut conn := HTTPConnection{
base_url: 'https://api.example.com'
cache: CacheConfig{
enabled: true
// Add other cache configuration as needed
}
}
16 changes: 16 additions & 0 deletions crystallib/clients/httpconnection/request.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ pub mut:
debug bool
dataformat DataFormat
}




// // set a custom hdeader on the request
// // ```v
// // import net.http { Header }
// // header: http.new_header(
// // key: .content_type
// // value: 'application/json'
// // )
// // )!
// // ```
// fn (mut r Request) header_set(header Header) {
// r.header = header
// }
8 changes: 8 additions & 0 deletions crystallib/clients/openai/.heroscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

!!hero_code.generate_client
name:'openai'
classname:'OpenAIClient'
singleton:0
default:1
hasconfig:1
reset:0
23 changes: 0 additions & 23 deletions crystallib/clients/openai/actions.v

This file was deleted.

3 changes: 2 additions & 1 deletion crystallib/clients/openai/audio.v
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ fn (mut f OpenAIClient[Config]) create_audio_request(args AudioArgs, endpoint st
req := httpconnection.Request{
prefix: endpoint
}
r := f.connection.post_multi_part(req, form)!
mut conn := f.connection()!
r := conn.post_multi_part(req, form)!
if r.status_code != 200 {
return error('got error from server: ${r.body}')
}
Expand Down
3 changes: 2 additions & 1 deletion crystallib/clients/openai/completions.v
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ pub fn (mut f OpenAIClient[Config]) chat_completion(model_type ModelType, msgs M
m.messages << mr
}
data := json.encode(m)
r := f.connection.post_json_str(prefix: 'chat/completions', data: data)!
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'chat/completions', data: data)!

res := json.decode(ChatCompletion, r)!
return res
Expand Down
3 changes: 2 additions & 1 deletion crystallib/clients/openai/embeddings.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn (mut f OpenAIClient[Config]) create_embeddings(args EmbeddingCreateArgs)
user: args.user
}
data := json.encode(req)
r := f.connection.post_json_str(prefix: 'embeddings', data: data)!
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'embeddings', data: data)!
return json.decode(EmbeddingResponse, r)!
}
64 changes: 0 additions & 64 deletions crystallib/clients/openai/factory.v

This file was deleted.

15 changes: 10 additions & 5 deletions crystallib/clients/openai/files.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn (mut f OpenAIClient[Config]) upload_file(args FileUploadArgs) !File {
req := httpconnection.Request{
prefix: 'files'
}
r := f.connection.post_multi_part(req, form)!
mut conn := f.connection()!
r := conn.post_multi_part(req, form)!
if r.status_code != 200 {
return error('got error from server: ${r.body}')
}
Expand All @@ -67,24 +68,28 @@ pub fn (mut f OpenAIClient[Config]) upload_file(args FileUploadArgs) !File {

// list all files in client org
pub fn (mut f OpenAIClient[Config]) list_files() !Files {
r := f.connection.get(prefix: 'files')!
mut conn := f.connection()!
r := conn.get(prefix: 'files')!
return json.decode(Files, r)!
}

// deletes a file
pub fn (mut f OpenAIClient[Config]) delete_file(file_id string) !DeleteResp {
r := f.connection.delete(prefix: 'files/' + file_id)!
mut conn := f.connection()!
r := conn.delete(prefix: 'files/' + file_id)!
return json.decode(DeleteResp, r)!
}

// returns a single file metadata
pub fn (mut f OpenAIClient[Config]) get_file(file_id string) !File {
r := f.connection.get(prefix: 'files/' + file_id)!
mut conn := f.connection()!
r := conn.get(prefix: 'files/' + file_id)!
return json.decode(File, r)!
}

// returns the content of a specific file
pub fn (mut f OpenAIClient[Config]) get_file_content(file_id string) !string {
r := f.connection.get(prefix: 'files/' + file_id + '/content')!
mut conn := f.connection()!
r := conn.get(prefix: 'files/' + file_id + '/content')!
return r
}
15 changes: 10 additions & 5 deletions crystallib/clients/openai/fine_tunes.v
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,36 @@ pub mut:
// creates a new fine-tune based on an already uploaded file
pub fn (mut f OpenAIClient[Config]) create_fine_tune(args FineTuneCreateArgs) !FineTune {
data := json.encode(args)
r := f.connection.post_json_str(prefix: 'fine-tunes', data: data)!
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'fine-tunes', data: data)!

return json.decode(FineTune, r)!
}

// returns all fine-tunes in this account
pub fn (mut f OpenAIClient[Config]) list_fine_tunes() !FineTuneList {
r := f.connection.get(prefix: 'fine-tunes')!
mut conn := f.connection()!
r := conn.get(prefix: 'fine-tunes')!
return json.decode(FineTuneList, r)!
}

// get a single fine-tune information
pub fn (mut f OpenAIClient[Config]) get_fine_tune(fine_tune string) !FineTune {
r := f.connection.get(prefix: 'fine-tunes/' + fine_tune)!
mut conn := f.connection()!
r := conn.get(prefix: 'fine-tunes/' + fine_tune)!
return json.decode(FineTune, r)!
}

// cancel a fine-tune that didn't finish yet
pub fn (mut f OpenAIClient[Config]) cancel_fine_tune(fine_tune string) !FineTune {
r := f.connection.post_json_str(prefix: 'fine-tunes/' + fine_tune + '/cancel')!
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'fine-tunes/' + fine_tune + '/cancel')!
return json.decode(FineTune, r)!
}

// returns all events for a fine tune in this account
pub fn (mut f OpenAIClient[Config]) list_fine_tune_events(fine_tune string) !FineTuneEventList {
r := f.connection.get(prefix: 'fine-tunes/' + fine_tune + '/events')!
mut conn := f.connection()!
r := conn.get(prefix: 'fine-tunes/' + fine_tune + '/events')!
return json.decode(FineTuneEventList, r)!
}
9 changes: 6 additions & 3 deletions crystallib/clients/openai/images.v
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ pub fn (mut f OpenAIClient[Config]) create_image(args ImageCreateArgs) !Images {
user: args.user
}
data := json.encode(request)
r := f.connection.post_json_str(prefix: 'images/generations', data: data)!
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'images/generations', data: data)!
return json.decode(Images, r)!
}

Expand Down Expand Up @@ -148,7 +149,8 @@ pub fn (mut f OpenAIClient[Config]) create_edit_image(args ImageEditArgs) !Image
req := httpconnection.Request{
prefix: 'images/edits'
}
r := f.connection.post_multi_part(req, form)!
mut conn := f.connection()!
r := conn.post_multi_part(req, form)!
if r.status_code != 200 {
return error('got error from server: ${r.body}')
}
Expand Down Expand Up @@ -181,7 +183,8 @@ pub fn (mut f OpenAIClient[Config]) create_variation_image(args ImageVariationAr
req := httpconnection.Request{
prefix: 'images/variations'
}
r := f.connection.post_multi_part(req, form)!
mut conn := f.connection()!
r := conn.post_multi_part(req, form)!
if r.status_code != 200 {
return error('got error from server: ${r.body}')
}
Expand Down
Loading

0 comments on commit 05f9ae7

Please sign in to comment.