Sign in

    openai

    Download zip
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    11 months ago
    Downloads
    65

    #OpenAI MoonBit Client

    A MoonBit client library for interacting with OpenAI's API, supporting chat completions with function calling and streaming responses.

    #Features

    • 🤖 Chat Completions: Full support for OpenAI's chat completions API
    • 🔧 Function Calling: Native support for tools and function calling
    • 📡 Streaming: Real-time streaming responses
    • 🔗 Flexible Base URLs: Works with OpenAI, OpenRouter, and other compatible APIs
    • 🔐 Environment Variables: Automatic API key detection from environment
    • 🛡️ Type Safety: Full type safety with MoonBit's type system

    #Installation

    Add this package to your MoonBit project:

    moon add tonyfettes/openai

    #Quick Start

    #Basic Chat Completion

    fn main {
    let client = @openai.Client::new()
    let messages = [
    @openai.system_message(content="You are a helpful assistant."),
    @openai.user_message(content="Hello! How are you?"),
    ]
    let response = client.chat.completions.create(
    model="gpt-4",
    messages~,
    )
    match response.choices {
    [choice, ..] =>
    match choice.message.content {
    Some(content) => println(content)
    None => println("No response received")
    }
    [] => println("No choices in response")
    }
    }

    #Function Calling

    fn main {
    let client = @openai.Client::new()
    let messages = [
    @openai.system_message(content="You are a helpful assistant."),
    @openai.user_message(
    content="Can you please tell me what's the sum of 5 and 10 by calling the adder tool?",
    ),
    ]

    // Define a tool for adding numbers
    let tools = [
    @openai.tool(
    name="adder",
    description="Adds two numbers",
    parameters={
    "type": "object",
    "properties": {
    "a": { "type": "number" },
    "b": { "type": "number" }
    },
    "required": ["a", "b"],
    }
    ),
    ]

    let response = client.chat.completions.create(
    model="gpt-4",
    messages~,
    tools~,
    )

    // Process the response and handle tool calls
    let choices = response.choices
    if choices.length() > 0 {
    let choice = choices[0]
    let message = choice.message

    // Add the assistant's message to conversation
    messages.push(message.to_param())

    // Handle tool calls
    let tool_calls = message.tool_calls
    for tool_call in tool_calls {
    if tool_call.function.name == "adder" {
    let result = match @json.parse(tool_call.function.arguments) {
    { "a": Number(a, ..), "b": Number(b, ..), .. } => "Result: \{a + b}"
    _ => "Invalid arguments"
    }

    // Add tool response to conversation
    messages.push(
    @openai.tool_message(content=result, tool_call_id=tool_call.id),
    )
    }
    }

    // Get final response
    let final_response = client.chat.completions.create(
    model="gpt-4",
    messages~,
    tools~,
    )

    match final_response.choices[0].message.content {
    Some(content) => println(content)
    None => println("No final response")
    }
    }
    }

    #Configuration

    #API Key

    The client automatically looks for your API key in the following order:

    1. Explicitly passed api_key parameter
    2. OPENAI_API_KEY environment variable
    3. OPENAI_API_KEY in .env file

    // Explicit API key
    let client = @openai.Client::new(api_key="your-api-key-here")

    // Using environment variable (recommended)
    let client = @openai.Client::new()

    #Custom Base URL

    You can use the client with OpenRouter or other compatible APIs:

    // OpenRouter
    let client = @openai.Client::new(base_url="https://openrouter.ai/api/v1")

    // Azure OpenAI
    let client = @openai.Client::new(base_url="https://your-resource.openai.azure.com/openai/deployments/your-deployment")

    // Local server
    let client = @openai.Client::new(base_url="http://localhost:8000/v1")

    HttpClient

    pub(open) trait HttpClient {
    async fetch(self : Self, url~ : String, method_~ : HttpMethod, headers~ : Map[String, String], body~ : String) -> Response
    }

    ChatCompletion

    pub struct ChatCompletion {
    id : String
    choices : Array[ChatCompletionChoice]
    created : Int
    model : String
    usage : CompletionUsage?
    system_fingerprint : String?
    service_tier : ChatCompletionServiceTier?
    }

    ChatCompletionAssistantMessageParam

    pub struct ChatCompletionAssistantMessageParam {
    content : String?
    name : String?
    tool_calls : Array[ChatCompletionMessageToolCall]
    function_call : ChatCompletionMessageFunctionCall?
    }

    ChatCompletionAudio

    pub struct ChatCompletionAudio {
    id : String?
    }

    Audio information for chat completions

    ChatCompletionAudioParam

    pub struct ChatCompletionAudioParam {
    voice : String
    format : String
    }

    Audio parameters for chat completion

    ChatCompletionChoice

    pub struct ChatCompletionChoice {
    finish_reason : ChatCompletionChoiceFinishReason
    index : Int
    message : ChatCompletionMessage
    logprobs : ChatCompletionChoiceLogprobs?
    }

    ChatCompletionChoiceFinishReason

    pub enum ChatCompletionChoiceFinishReason {
    Stop
    Length
    ToolCalls
    ContentFilter
    FunctionCall
    }

    ChatCompletionChoiceLogprobs

    pub struct ChatCompletionChoiceLogprobs {
    content : Array[ChatCompletionTokenLogprob]?
    refusal : Array[ChatCompletionTokenLogprob]?
    }

    Logprobs information for chat completion choices

    ChatCompletionChunk

    pub struct ChatCompletionChunk {
    id : String
    choices : Array[ChatCompletionChunkChoice]
    created : Int
    model : String
    usage : CompletionUsage?
    system_fingerprint : String?
    service_tier : ChatCompletionServiceTier?
    }

    ChatCompletionChunkChoice

    pub struct ChatCompletionChunkChoice {
    index : Int
    delta : ChatCompletionChunkChoiceDelta
    finish_reason : ChatCompletionChoiceFinishReason?
    }

    ChatCompletionChunkChoiceDelta

    pub struct ChatCompletionChunkChoiceDelta {
    content : String?
    role : ChatCompletionRole?
    tool_calls : Array[ChatCompletionChunkChoiceDeltaToolCall]?
    }

    ChatCompletionChunkChoiceDeltaToolCall

    pub struct ChatCompletionChunkChoiceDeltaToolCall {
    index : Int
    id : String?
    function : ChatCompletionChunkChoiceDeltaToolCallFunction
    }

    ChatCompletionChunkChoiceDeltaToolCallFunction

    pub struct ChatCompletionChunkChoiceDeltaToolCallFunction {
    arguments : String?
    name : String?
    }

    ChatCompletionContentPart

    Content part types for multimodal messages

    ChatCompletionContentPartFile

    pub struct ChatCompletionContentPartFile {
    file : ChatCompletionContentPartFileFile
    }

    File content part

    ChatCompletionContentPartFileFile

    pub struct ChatCompletionContentPartFileFile {
    file_id : String
    }

    File information

    ChatCompletionContentPartImage

    pub struct ChatCompletionContentPartImage {
    image_url : ChatCompletionContentPartImageImageURL
    }

    Image URL content part

    ChatCompletionContentPartImageImageURL

    pub struct ChatCompletionContentPartImageImageURL {
    url : String
    detail : String?
    }

    Image URL information

    ChatCompletionContentPartInputAudio

    pub struct ChatCompletionContentPartInputAudio {
    input_audio : ChatCompletionContentPartInputAudioInputAudio
    }

    Input audio content part

    ChatCompletionContentPartInputAudioInputAudio

    pub struct ChatCompletionContentPartInputAudioInputAudio {
    data : String
    format : String
    }

    Input audio information

    ChatCompletionContentPartText

    pub struct ChatCompletionContentPartText {
    text : String
    }

    Text content part

    ChatCompletionMessage

    pub struct ChatCompletionMessage {
    content : String?
    refusal : String?
    tool_calls : Array[ChatCompletionMessageToolCall]
    annotations : Array[ChatCompletionMessageAnnotation]
    audio : ChatCompletionAudio?
    function_call : ChatCompletionMessageFunctionCall?
    }

    ChatCompletionMessage::to_param

    ChatCompletionMessageAnnotation

    pub struct ChatCompletionMessageAnnotation {
    type_ : String
    text : String
    file_citation : ChatCompletionMessageAnnotationFileCitation?
    file_path : ChatCompletionMessageAnnotationFilePath?
    start_index : Int
    end_index : Int
    }

    Message annotation for web search results

    ChatCompletionMessageAnnotationFileCitation

    pub struct ChatCompletionMessageAnnotationFileCitation {
    file_id : String
    quote : String
    }

    File citation annotation

    ChatCompletionMessageAnnotationFilePath

    pub struct ChatCompletionMessageAnnotationFilePath {
    file_id : String
    }

    File path annotation

    ChatCompletionMessageFunctionCall

    pub struct ChatCompletionMessageFunctionCall {
    arguments : String
    name : String
    }

    Deprecated function call format (replaced by tool_calls)

    ChatCompletionMessageParam

    ChatCompletionMessageToolCall

    pub struct ChatCompletionMessageToolCall {
    id : String
    function : Function
    }

    ChatCompletionPredictionContent

    pub struct ChatCompletionPredictionContent {
    type_ : String
    content : String
    }

    Prediction content for static predicted output

    ChatCompletionResponseFormat

    pub enum ChatCompletionResponseFormat {
    JsonObject
    JsonSchema(ChatCompletionResponseFormatJsonSchema)
    Text
    }

    Response format types

    ChatCompletionResponseFormatJsonSchema

    pub struct ChatCompletionResponseFormatJsonSchema {
    name : String
    description : String?
    schema : Map[String, Json]
    strict : Bool?
    }

    JSON schema for response format

    ChatCompletionRole

    pub enum ChatCompletionRole {
    Developer
    System
    User
    Assistant
    Tool
    }

    ChatCompletionServiceTier

    pub enum ChatCompletionServiceTier {
    Auto
    Default
    Flex
    Scale
    Priority
    }

    Service tier types for chat completion requests

    ChatCompletionStopParam

    pub enum ChatCompletionStopParam {
    String(String)
    Array(Array[String])
    }

    Stop sequences parameter

    ChatCompletionStreamOptions

    pub struct ChatCompletionStreamOptions {
    include_usage : Bool?
    }

    Streaming options for chat completion

    ChatCompletionSystemMessageParam

    pub struct ChatCompletionSystemMessageParam {
    content : String
    name : String?
    }

    ChatCompletionTokenLogprob

    pub struct ChatCompletionTokenLogprob {
    token : String
    logprob : Double
    bytes : Array[Int]?
    top_logprobs : Array[ChatCompletionTopLogprob]
    }

    Token logprob information

    ChatCompletionToolMessageParam

    pub struct ChatCompletionToolMessageParam {
    content : String
    tool_call_id : String
    }

    ChatCompletionToolParam

    type ChatCompletionToolParam

    ChatCompletionTopLogprob

    pub struct ChatCompletionTopLogprob {
    token : String
    logprob : Double
    bytes : Array[Int]?
    }

    Top logprob information

    ChatCompletionUserMessageParam

    pub struct ChatCompletionUserMessageParam {
    content : String
    name : String?
    }

    ChatCompletionWebSearchOptions

    pub struct ChatCompletionWebSearchOptions {
    max_results : Int?
    }

    Web search options

    ChatCompletionsService

    pub struct ChatCompletionsService {
    // private fields
    }

    ChatCompletionsService::create

    async fn ChatCompletionsService::create(self : ChatCompletionsService, messages~ : Array[ChatCompletionMessageParam], model~ : String, tools? : Array[ChatCompletionToolParam], frequency_penalty? : Double, logprobs? : Bool, max_completion_tokens? : Int, max_tokens? : Int, n? : Int, presence_penalty? : Double, seed? : Int, store? : Bool, temperature? : Double, top_logprobs? : Int, top_p? : Double, parallel_tool_calls? : Bool, prompt_cache_key? : String, safety_identifier? : String, user? : String, audio? : ChatCompletionAudioParam, logit_bias? : Map[String, Int], metadata? : Map[String, String], modalities? : Array[String], reasoning_effort? : ReasoningEffort, service_tier? : ChatCompletionServiceTier, stop? : ChatCompletionStopParam, stream_options? : ChatCompletionStreamOptions, verbosity? : Verbosity, prediction? : ChatCompletionPredictionContent, response_format? : ChatCompletionResponseFormat, web_search_options? : ChatCompletionWebSearchOptions) -> ChatCompletion

    ChatCompletionsService::stream

    ChatService

    pub struct ChatService {
    completions : ChatCompletionsService
    }

    Client

    pub struct Client {
    chat : ChatService
    }

    Client::new

    fn Client::new(http_client~ : &HttpClient, base_url? : String, api_key? : String) -> Client

    CompletionUsage

    pub struct CompletionUsage {
    completion_tokens : Int
    prompt_tokens : Int
    total_tokens : Int
    }

    Function

    pub struct Function {
    arguments : String
    name : String
    }

    HttpMethod

    pub(all) enum HttpMethod {
    Post
    }

    Reader

    type Reader[T]

    Reader::new

    fn[T] Reader::new(f : async () -> T?) -> Reader[T]

    Reader::read

    async fn[T] Reader::read(self : Reader[T]) -> T?

    ReasoningEffort

    pub enum ReasoningEffort {
    Minimal
    Low
    Medium
    High
    }

    Reasoning effort levels for reasoning models

    Response

    pub struct Response {
    status_code : Int
    reason_phrase : String
    headers : Array[String]
    body : Reader[String]
    }

    Response::new

    fn Response::new(status_code~ : Int, reason_phrase~ : String, headers~ : Array[String], body~ : Reader[String]) -> Response

    Verbosity

    pub enum Verbosity {
    Low
    Medium
    High
    }

    Verbosity levels for model responses

    assistant_message

    fn assistant_message(content? : String, tool_calls? : Array[ChatCompletionMessageToolCall], name? : String) -> ChatCompletionMessageParam

    audio_content_part

    fn audio_content_part(data : String, format : String) -> ChatCompletionContentPart

    file_content_part

    fn file_content_part(file_id : String) -> ChatCompletionContentPart

    image_content_part

    fn image_content_part(url : String, detail? : String) -> ChatCompletionContentPart

    system_message

    fn system_message(content~ : String, name? : String) -> ChatCompletionMessageParam

    text_content_part

    fn text_content_part(text : String) -> ChatCompletionContentPart

    tool

    fn tool(name~ : String, description~ : String, parameters~ : Map[String, Json], strict? : Bool) -> ChatCompletionToolParam

    tool_message

    fn tool_message(content~ : String, tool_call_id~ : String) -> ChatCompletionMessageParam

    user_message

    fn user_message(content~ : String, name? : String) -> ChatCompletionMessageParam