Skip to content

clients.openai #

OpenAI

An implementation of an OpenAI client using Vlang.

Supported methods

  • List available models
  • Chat Completion
  • Translate Audio
  • Transcribe Audio
  • Create image based on prompt
  • Edit an existing image
  • Create variation of an image

Usage

To use the client you need a OpenAi key which can be generated from here.

The key should be exposed in an environment variable as following:

export OPENAI_API_KEY=<your-api-key>

To get a new instance of the client:

import freeflowuniverse.crystallib.clients.openai

ai_cli := openai.new()!

Then it is possible to perform all the listed operations:

// listing models
models := ai_cli.list_models()!

// creating a new chat completion

mut msg := []op.Message{}
msg << op.Message{
    role: op.RoleType.user
    content: 'Say this is a test!'
}
mut msgs := op.Messages{
    messages: msg
}
res := ai_cli.chat_completion(op.ModelType.gpt_3_5_turbo, msgs)!

fn configure #

fn configure(instance_ string) !

get a new OpenAI client, will create if it doesn't exist or ask for new configuration

fn get #

fn get(instance string, cfg Config) !OpenAIClient[Config]

fn heroplay #

fn heroplay(mut plbook playbook.PlayBook) !

run heroscript starting from path, text or giturl

 !!OpenAIclient.define
     name:'default'
       openaikey: ''
     description:'...'
```	

fn (OpenAIClient[Config]) cancel_fine_tune #

fn (mut f OpenAIClient[Config]) cancel_fine_tune(fine_tune string) !FineTune

cancel a fine-tune that didn't finish yet

fn (OpenAIClient[Config]) chat_completion #

fn (mut f OpenAIClient[Config]) chat_completion(model_type ModelType, msgs Messages) !ChatCompletion

creates a new chat completion given a list of messages each message consists of message content and the role of the author

fn (OpenAIClient[Config]) create_edit_image #

fn (mut f OpenAIClient[Config]) create_edit_image(args ImageEditArgs) !Images

edit images generation given a prompt and an existing image image needs to be in PNG format and transparent or else a mask of the same size needs to be specified to indicate where the image should be in the generated image the amount of images returned is specified by num_images

fn (OpenAIClient[Config]) create_embeddings #

fn (mut f OpenAIClient[Config]) create_embeddings(args EmbeddingCreateArgs) !EmbeddingResponse

fn (OpenAIClient[Config]) create_fine_tune #

fn (mut f OpenAIClient[Config]) create_fine_tune(args FineTuneCreateArgs) !FineTune

creates a new fine-tune based on an already uploaded file

fn (OpenAIClient[Config]) create_image #

fn (mut f OpenAIClient[Config]) create_image(args ImageCreateArgs) !Images

Create new images generation given a prompt the amount of images returned is specified by num_images

fn (OpenAIClient[Config]) create_moderation #

fn (mut f OpenAIClient[Config]) create_moderation(input string, model ModerationModel) !ModerationResponse

fn (OpenAIClient[Config]) create_tranlation #

fn (mut f OpenAIClient[Config]) create_tranlation(args AudioArgs) !AudioResponse

create translation to english from an audio file supported audio formats are mp3, mp4, mpeg, mpga, m4a, wav, or webm

fn (OpenAIClient[Config]) create_transcription #

fn (mut f OpenAIClient[Config]) create_transcription(args AudioArgs) !AudioResponse

create transcription from an audio file supported audio formats are mp3, mp4, mpeg, mpga, m4a, wav, or webm

fn (OpenAIClient[Config]) create_variation_image #

fn (mut f OpenAIClient[Config]) create_variation_image(args ImageVariationArgs) !Images

create variations of the given image image needs to be in PNG format the amount of images returned is specified by num_images

fn (OpenAIClient[Config]) delete_file #

fn (mut f OpenAIClient[Config]) delete_file(file_id string) !DeleteResp

deletes a file

fn (OpenAIClient[Config]) get_file #

fn (mut f OpenAIClient[Config]) get_file(file_id string) !File

returns a single file metadata

fn (OpenAIClient[Config]) get_file_content #

fn (mut f OpenAIClient[Config]) get_file_content(file_id string) !string

returns the content of a specific file

fn (OpenAIClient[Config]) get_fine_tune #

fn (mut f OpenAIClient[Config]) get_fine_tune(fine_tune string) !FineTune

get a single fine-tune information

fn (OpenAIClient[Config]) get_model #

fn (mut f OpenAIClient[Config]) get_model(model string) !Model

returns details of a model using the model id

fn (OpenAIClient[Config]) list_files #

fn (mut f OpenAIClient[Config]) list_files() !Files

list all files in client org

fn (OpenAIClient[Config]) list_fine_tune_events #

fn (mut f OpenAIClient[Config]) list_fine_tune_events(fine_tune string) !FineTuneEventList

returns all events for a fine tune in this account

fn (OpenAIClient[Config]) list_fine_tunes #

fn (mut f OpenAIClient[Config]) list_fine_tunes() !FineTuneList

returns all fine-tunes in this account

fn (OpenAIClient[Config]) list_models #

fn (mut f OpenAIClient[Config]) list_models() !Models

list current models available in Open AI

fn (OpenAIClient[Config]) upload_file #

fn (mut f OpenAIClient[Config]) upload_file(args FileUploadArgs) !File

upload file to client org, usually used for fine tuning

enum AudioRespType #

enum AudioRespType {
	json
	text
	srt
	verbose_json
	vtt
}

enum EmbeddingModel #

enum EmbeddingModel {
	text_embedding_ada
}

enum ImageRespType #

enum ImageRespType {
	url
	b64_json
}

enum ImageSize #

enum ImageSize {
	size_256_256
	size_512_512
	size_1024_1024
}

enum ModelType #

enum ModelType {
	gpt_3_5_turbo
	gpt_4
	gpt_4_0613
	gpt_4_32k
	gpt_4_32k_0613
	gpt_3_5_turbo_0613
	gpt_3_5_turbo_16k
	gpt_3_5_turbo_16k_0613
	whisper_1
}

enum ModerationModel #

enum ModerationModel {
	text_moderation_latest
	text_moderation_stable
}

enum RoleType #

enum RoleType {
	system
	user
	assistant
	function
}

struct AudioArgs #

struct AudioArgs {
pub mut:
	filepath        string
	prompt          string
	response_format AudioRespType
	temperature     int
	language        string
}

struct AudioResponse #

struct AudioResponse {
pub mut:
	text string
}

struct ChatCompletion #

struct ChatCompletion {
pub mut:
	id      string
	object  string
	created u32
	choices []Choice
	usage   Usage
}

struct Choice #

struct Choice {
pub mut:
	index         int
	message       MessageRaw
	finish_reason string
}

struct Config #

@[params]
struct Config {
pub mut:
	openaikey   string @[secret]
	description string
}

struct DeleteResp #

struct DeleteResp {
pub mut:
	id      string
	object  string
	deleted bool
}

struct Embedding #

struct Embedding {
pub mut:
	object    string
	embedding []f32
	index     int
}

struct EmbeddingCreateArgs #

@[params]
struct EmbeddingCreateArgs {
	input []string       @[required]
	model EmbeddingModel @[required]
	user  string
}

struct EmbeddingCreateRequest #

struct EmbeddingCreateRequest {
	input []string
	model string
	user  string
}

struct EmbeddingResponse #

struct EmbeddingResponse {
pub mut:
	object string
	data   []Embedding
	model  string
	usage  Usage
}

struct File #

struct File {
pub mut:
	id         string
	object     string
	bytes      int
	created_at int
	filename   string
	purpose    string
}

struct FileUploadArgs #

@[params]
struct FileUploadArgs {
pub:
	filepath string
	purpose  string
}

struct Files #

struct Files {
pub mut:
	data []File
}

struct FineTune #

struct FineTune {
pub:
	id               string
	object           string
	model            string
	created_at       int
	events           []FineTuneEvent
	fine_tuned_model string
	hyperparams      FineTuneHyperParams
	organization_id  string
	result_files     []File
	status           string
	validation_files []File
	training_files   []File
	updated_at       int
}

struct FineTuneCreateArgs #

@[params]
struct FineTuneCreateArgs {
pub mut:
	training_file                  string @[required]
	model                          string
	n_epochs                       int = 4
	batch_size                     int
	learning_rate_multiplier       f32
	prompt_loss_weight             f64
	compute_classification_metrics bool
	suffix                         string
}

struct FineTuneEvent #

struct FineTuneEvent {
pub:
	object     string
	created_at int
	level      string
	message    string
}

struct FineTuneEventList #

struct FineTuneEventList {
pub:
	object string
	data   []FineTuneEvent
}

struct FineTuneHyperParams #

struct FineTuneHyperParams {
pub:
	batch_size               int
	learning_rate_multiplier f64
	n_epochs                 int
	prompt_loss_weight       f64
}

struct FineTuneList #

struct FineTuneList {
pub:
	object string
	data   []FineTune
}

struct ImageCreateArgs #

struct ImageCreateArgs {
pub mut:
	prompt     string
	num_images int
	size       ImageSize
	format     ImageRespType
	user       string
}

struct ImageEditArgs #

struct ImageEditArgs {
pub mut:
	image_path string
	mask_path  string
	prompt     string
	num_images int
	size       ImageSize
	format     ImageRespType
	user       string
}

struct ImageRequest #

struct ImageRequest {
pub mut:
	prompt          string
	n               int
	size            string
	response_format string
	user            string
}

struct ImageResponse #

struct ImageResponse {
pub mut:
	url      string
	b64_json string
}

struct ImageVariationArgs #

struct ImageVariationArgs {
pub mut:
	image_path string
	num_images int
	size       ImageSize
	format     ImageRespType
	user       string
}

struct Images #

struct Images {
pub mut:
	created int
	data    []ImageResponse
}

struct Message #

struct Message {
pub mut:
	role    RoleType
	content string
}

struct MessageRaw #

struct MessageRaw {
pub mut:
	role    string
	content string
}

struct Messages #

struct Messages {
pub mut:
	messages []Message
}

struct Model #

struct Model {
pub mut:
	id         string
	created    int
	object     string
	owned_by   string
	root       string
	parent     string
	permission []ModelPermission
}

struct ModelPermission #

struct ModelPermission {
pub mut:
	id                   string
	created              int
	object               string
	allow_create_engine  bool
	allow_sampling       bool
	allow_logprobs       bool
	allow_search_indices bool
	allow_view           bool
	allow_fine_tuning    bool
	organization         string
	is_blocking          bool
}

struct Models #

struct Models {
pub mut:
	data []Model
}

struct ModerationRequest #

@[params]
struct ModerationRequest {
mut:
	input string
	model string
}

struct ModerationResponse #

struct ModerationResponse {
pub mut:
	id      string
	model   string
	results []ModerationResult
}

struct ModerationResult #

struct ModerationResult {
pub mut:
	categories      ModerationResultCategories
	category_scores ModerationResultCategoryScores
	flagged         bool
}

struct ModerationResultCategories #

struct ModerationResultCategories {
pub mut:
	sexual                 bool
	hate                   bool
	harassment             bool
	selfharm               bool @[json: 'self-harm']
	sexual_minors          bool @[json: 'sexual/minors']
	hate_threatening       bool @[json: 'hate/threatening']
	violence_graphic       bool @[json: 'violence/graphic']
	selfharm_intent        bool @[json: 'self-harm/intent']
	selfharm_instructions  bool @[json: 'self-harm/instructions']
	harassment_threatening bool @[json: 'harassment/threatening']
	violence               bool
}

struct ModerationResultCategoryScores #

struct ModerationResultCategoryScores {
pub mut:
	sexual                 f32
	hate                   f32
	harassment             f32
	selfharm               f32 @[json: 'self-harm']
	sexual_minors          f32 @[json: 'sexual/minors']
	hate_threatening       f32 @[json: 'hate/threatening']
	violence_graphic       f32 @[json: 'violence/graphic']
	selfharm_intent        f32 @[json: 'self-harm/intent']
	selfharm_instructions  f32 @[json: 'self-harm/instructions']
	harassment_threatening f32 @[json: 'harassment/threatening']
	violence               f32
}

struct OpenAIClient #

struct OpenAIClient[T] {
	base.BaseConfig[T]
pub mut:
	connection &httpconnection.HTTPConnection
}

import freeflowuniverse.crystallib.ui.console

struct Usage #

struct Usage {
pub mut:
	prompt_tokens     int
	completion_tokens int
	total_tokens      int
}