All Bundles

File

OpenAI file support

Derived from: EndPoint

Operations

Code example:

# upload tuning file
filename := "tuning.json";
data := FileReader->ReadFile(filename)->ToByteArray();
API.OpenAI.File->Create(filename, "fine-tune", data, token)->PrintLine();

# list files
files := API.OpenAI.File->ListFiles(token);
if(files <> Nil) {
  each(file in files) {
    name := file->GetFilename();
    id := file->GetId();
    "file='{$name}', id='{$id}'"->PrintLine();
  };
}
else {
  API.OpenAI.File->GetLastError()->PrintLine();
};

# start tuning job
file := API.OpenAI.File->LoadOrCreate(filename, "fine-tune", token);
name := file->GetFilename();
id := file->GetId();
"file='{$name}', id='{$id}'"->PrintLine();

tuning_job := Tuner->Create("gpt-3.5-turbo", id, token);
model_id := tuning_job->GetId();

# query model

assistant := Assistant->Create(model_id, token);
if(assistant = Nil) {
  Assistant->GetLastError()->PrintLine();
  return;
};

assistant_id := assistant->GetId();
assistant_name := assistant->GetName();
assistant_model := assistant->GetModel();
"Created: id='{$id}', name='{$name}', model='{$model}', files={$file_count}, tools={$tool_count}"->PrintLine();

# chat with tuned assistant
session := API.OpenAI.Chat.Conversation->New(Assistant->Load(assistant_id, token), true, token);
session->AddFunction("get_coach_by_year", Callback(JsonElement) ~ JsonElement);

done := false;
do {
  query := Console->ReadLine();
  if(query->Equals("/quit")) {
    session->Close();    
    done := true;
  }
  else {
    response := session->Send(query, "user");

    index := 0;
    role := response->GetRole();
    contents := response->GetContents();
    each(content in contents) {
      type := content->GetFirst()->As(String);
      value := content->GetSecond()->As(String);

      "{$index}: [{$role}, type='{$type}']: value='{$value}'"->PrintLine();
    };
  };
}
while(<>done);

session->Close();

Create

Upload a file that can be used across various endpoints

function : Create(name:String, content:Byte[], token:String) ~ Bool
Parameters
NameTypeDescription
nameStringfile object name
contentByte[]file content
tokenStringAPI token

Return
TypeDescription
Boolstring representation

Upload a file that can be used across various endpoints

function : Create(name:String, purpose:String, content:Byte[], token:String) ~ Bool
Parameters
NameTypeDescription
nameStringfile object name
purposeStringfile purpose 'assistants', 'vision' or 'fine-tune'
contentByte[]file content
tokenStringAPI token

Return
TypeDescription
Boolstring representation

Delete

Deletes a file

function : Delete(id:String, token:String) ~ Bool
Parameters
NameTypeDescription
idStringfile ID
tokenStringAPI token

Return
TypeDescription
Booltrue if successful, false otherwise

GetBytes

Get the size of the file

method : public : GetBytes() ~ Int
Return
TypeDescription
Intsize of the file

GetCreatedAt

Unix timestamp (in seconds) of when the object instance was created

method : public : GetCreatedAt() ~ Int
Return
TypeDescription
Inttime with the object instance was created

GetFilename

Get the name of the file

method : public : GetFilename() ~ String
Return
TypeDescription
Stringname of the file

GetId

Get object instance API ID

method : public : GetId() ~ String
Return
TypeDescription
Stringinstance ID

GetObject

Get the object type

method : public : GetObject() ~ String
Return
TypeDescription
Stringobject type

GetPurpose

Get the purpose

method : public : GetPurpose() ~ String
Return
TypeDescription
Stringpurpose

ListFiles

Loads a list available OpenAI files

function : ListFiles(token:String) ~ Vector<API.OpenAI.File>
Parameters
NameTypeDescription
tokenStringAPI token

Return
TypeDescription
File<>file reference

Load

Loads a file

function : Load(id:String, token:String) ~ API.OpenAI.File
Parameters
NameTypeDescription
idStringfile ID
tokenStringAPI token

Return
TypeDescription
Filefile reference

LoadByName

Loads an OpenAI file from the local filesystem

function : LoadByName(filename:String, token:String) ~ API.OpenAI.File
Parameters
NameTypeDescription
filenameStringlocal file path
tokenStringAPI token

Return
TypeDescription
Filefile reference

LoadOrCreate

Loads or creates an OpenAI file from the local filesystem

function : LoadOrCreate(filename:String, token:String) ~ API.OpenAI.File
Parameters
NameTypeDescription
filenameStringlocal file path
tokenStringAPI token

Return
TypeDescription
Filefile reference

Loads or creates an OpenAI file from the local filesystem

function : LoadOrCreate(filename:String, purpose:String, token:String) ~ API.OpenAI.File
Parameters
NameTypeDescription
filenameStringlocal file path
purposeStringfile purpose 'assistants', 'vision' or 'fine-tune'
tokenStringAPI token

Return
TypeDescription
Filefile reference

Retrieve

Returns the contents of the specified file

function : Retrieve(id:String, token:String) ~ Byte[]
Parameters
NameTypeDescription
idStringfile ID
tokenStringAPI token

Return
TypeDescription
Byte[]file content

ToString

String representation of the object

method : public : ToString() ~ String
Return
TypeDescription
Stringstring representation