const DEFAULT_TIMEOUT_NS
pub const DEFAULT_TIMEOUT_NS = 60000000000用途
常量 DEFAULT_TIMEOUT_NS,定义了相关的协议或算法配置参数。
#github.com/xulang-org/packages/openai扩展包 `openai #github.com/xulang-org/packages/openai`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
const DEFAULT_TIMEOUT_NSpub const DEFAULT_TIMEOUT_NS = 60000000000常量 DEFAULT_TIMEOUT_NS,定义了相关的协议或算法配置参数。
struct Clientpub struct Client {
pub base_url string
pub api_key string
pub timeout_ns int
pub max_retries int
pub extra_headers []string
}Openai 客户端会话实例,维护与服务端的连接并提供操作接口。
base_url stringapi_key stringtimeout_ns intmax_retries intextra_headers []stringstruct ToolCallpub struct ToolCall {
pub id string
pub kind string
pub name string
pub arguments string
}承载 id、kind、name、arguments 等属性的结构体。
id stringkind stringname stringarguments stringstruct ContentPartpub struct ContentPart {
pub kind string
pub text string
pub image_url string
pub detail string
}承载 kind、text、image_url、detail 等属性的结构体。
kind stringtext stringimage_url stringdetail stringstruct Messagepub struct Message {
pub role string
pub content string
pub parts []ContentPart
pub tool_call_id string
pub tool_calls []ToolCall
}承载 role、content、parts、tool_call_id、tool_calls 等属性的结构体。
role stringcontent stringparts []ContentParttool_call_id stringtool_calls []ToolCallstruct ToolDefpub struct ToolDef {
pub name string
pub description string
pub parameters_json string
}承载 name、description、parameters_json 等属性的结构体。
name stringdescription stringparameters_json stringstruct ChatRequestpub struct ChatRequest {
pub model string
pub messages []Message
pub tools []ToolDef
pub temperature f64
pub has_temperature bool
pub top_p f64
pub has_top_p bool
pub presence_penalty f64
pub has_presence_penalty bool
pub frequency_penalty f64
pub has_frequency_penalty bool
pub max_tokens int
pub max_completion_tokens int
pub seed int
pub has_seed bool
pub stop []string
pub tool_choice string
pub response_format_json string
pub extra_body_json string
pub include_usage bool
}承载 model、messages、tools、temperature、has_temperature 等属性的结构体。
model stringmessages []Messagetools []ToolDeftemperature f64has_temperature booltop_p f64has_top_p boolpresence_penalty f64has_presence_penalty boolfrequency_penalty f64has_frequency_penalty boolmax_tokens intmax_completion_tokens intseed inthas_seed boolstop []stringtool_choice stringresponse_format_json stringextra_body_json stringinclude_usage boolstruct CompletionRequestpub struct CompletionRequest {
pub model string
pub prompt string
pub temperature f64
pub has_temperature bool
pub top_p f64
pub has_top_p bool
pub max_tokens int
pub seed int
pub has_seed bool
pub stop []string
pub echo bool
pub extra_body_json string
}承载 model、prompt、temperature、has_temperature、top_p 等属性的结构体。
model stringprompt stringtemperature f64has_temperature booltop_p f64has_top_p boolmax_tokens intseed inthas_seed boolstop []stringecho boolextra_body_json stringstruct CompletionResponsepub struct CompletionResponse {
pub id string
pub model string
pub text string
pub finish_reason string
pub usage Usage
}Openai 服务端响应结果,包含状态码、响应头与正文数据。
id stringmodel stringtext stringfinish_reason stringusage Usagestruct Usagepub struct Usage {
pub prompt_tokens int
pub completion_tokens int
pub total_tokens int
}承载 prompt_tokens、completion_tokens、total_tokens 等属性的结构体。
prompt_tokens intcompletion_tokens inttotal_tokens intstruct ChatResponsepub struct ChatResponse {
pub id string
pub model string
pub content string
pub reasoning_content string
pub finish_reason string
pub tool_calls []ToolCall
pub usage Usage
}Openai 服务端响应结果,包含状态码、响应头与正文数据。
id stringmodel stringcontent stringreasoning_content stringfinish_reason stringtool_calls []ToolCallusage Usagestruct StreamDeltapub struct StreamDelta {
pub content string
pub reasoning_content string
pub finish_reason string
pub tool_calls []ToolCall
pub has_usage bool
pub usage Usage
pub done bool
}承载 content、reasoning_content、finish_reason、tool_calls、has_usage 等属性的结构体。
content stringreasoning_content stringfinish_reason stringtool_calls []ToolCallhas_usage boolusage Usagedone boolstruct EmbedRequestpub struct EmbedRequest {
pub model string
pub inputs []string
}承载 model、inputs 等属性的结构体。
model stringinputs []stringstruct EmbedResponsepub struct EmbedResponse {
pub model string
pub embeddings [][]f64
pub usage Usage
}Openai 服务端响应结果,包含状态码、响应头与正文数据。
model stringembeddings [][]f64usage Usagestruct ModelInfopub struct ModelInfo {
pub id string
pub object string
}承载 id、object 等属性的结构体。
id stringobject stringstruct ResponsesToolCallpub struct ResponsesToolCall {
pub id string
pub call_id string
pub name string
pub arguments string
}承载 id、call_id、name、arguments 等属性的结构体。
id stringcall_id stringname stringarguments stringstruct ResponsesRequestpub struct ResponsesRequest {
pub model string
pub input string
pub input_json string
pub instructions string
pub previous_response_id string
pub tools []ToolDef
pub temperature f64
pub has_temperature bool
pub top_p f64
pub has_top_p bool
pub max_output_tokens int
pub tool_choice string
pub extra_body_json string
}承载 model、input、input_json、instructions、previous_response_id 等属性的结构体。
model stringinput stringinput_json stringinstructions stringprevious_response_id stringtools []ToolDeftemperature f64has_temperature booltop_p f64has_top_p boolmax_output_tokens inttool_choice stringextra_body_json stringstruct ResponsesResponsepub struct ResponsesResponse {
pub id string
pub model string
pub status string
pub output_text string
pub tool_calls []ResponsesToolCall
pub usage Usage
}Openai 服务端响应结果,包含状态码、响应头与正文数据。
id stringmodel stringstatus stringoutput_text stringtool_calls []ResponsesToolCallusage Usagestruct ResponsesStreamEventpub struct ResponsesStreamEvent {
pub event_type string
pub text_delta string
pub response_id string
pub status string
pub tool_calls []ResponsesToolCall
pub has_usage bool
pub usage Usage
pub done bool
}承载 event_type、text_delta、response_id、status、tool_calls 等属性的结构体。
event_type stringtext_delta stringresponse_id stringstatus stringtool_calls []ResponsesToolCallhas_usage boolusage Usagedone boolstruct ImageGenResultpub struct ImageGenResult {
pub url string
pub b64_json string
pub revised_prompt string
}承载 url、b64_json、revised_prompt 等属性的结构体。
url stringb64_json stringrevised_prompt stringstruct RerankResultpub struct RerankResult {
pub index int
pub relevance_score f64
pub document string
}承载 index、relevance_score、document 等属性的结构体。
index intrelevance_score f64document stringstruct FileObjectpub struct FileObject {
pub id string
pub object string
pub bytes int
pub created_at int
pub filename string
pub purpose string
pub status string
}承载 id、object、bytes、created_at、filename 等属性的结构体。
id stringobject stringbytes intcreated_at intfilename stringpurpose stringstatus stringstruct BatchObjectpub struct BatchObject {
pub id string
pub object string
pub endpoint string
pub input_file_id string
pub output_file_id string
pub error_file_id string
pub completion_window string
pub status string
pub created_at int
pub request_total int
pub request_completed int
pub request_failed int
}承载 id、object、endpoint、input_file_id、output_file_id 等属性的结构体。
id stringobject stringendpoint stringinput_file_id stringoutput_file_id stringerror_file_id stringcompletion_window stringstatus stringcreated_at intrequest_total intrequest_completed intrequest_failed intnew_clientpub fn new_client(base_url string, api_key string) Client构造 client。
base_url stringapi_key stringClient — 该类型的值
new_deepseek_clientpub fn new_deepseek_client(api_key string) Client构造 deepseek client。
api_key stringClient — 该类型的值
new_openrouter_clientpub fn new_openrouter_client(api_key string) Client构造 openrouter client。
api_key stringClient — 该类型的值
new_groq_clientpub fn new_groq_client(api_key string) Client构造 groq client。
api_key stringClient — 该类型的值
new_xai_clientpub fn new_xai_client(api_key string) Client构造 xai client。
api_key stringClient — 该类型的值
new_together_clientpub fn new_together_client(api_key string) Client构造 together client。
api_key stringClient — 该类型的值
new_fireworks_clientpub fn new_fireworks_client(api_key string) Client构造 fireworks client。
api_key stringClient — 该类型的值
new_ollama_clientpub fn new_ollama_client(base_url string) Client构造 ollama client。
base_url stringClient — 该类型的值
new_cohere_clientpub fn new_cohere_client(api_key string) Client构造 cohere client。
api_key stringClient — 该类型的值
new_qwen_clientpub fn new_qwen_client(api_key string) Client构造 qwen client。
api_key stringClient — 该类型的值
new_mistral_clientpub fn new_mistral_client(api_key string) Client构造 mistral client。
api_key stringClient — 该类型的值
new_minimax_clientpub fn new_minimax_client(api_key string) Client构造 minimax client。
api_key stringClient — 该类型的值
($&Client) set_max_retriespub fn (c $&Client) set_max_retries(n int)$&Client 上的操作:设置 max retries。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。n int不返回值。
($&Client) add_headerpub fn (c $&Client) add_header(name string, value string)$&Client 上的操作:增加头。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。name stringvalue string不返回值。
(&Client) clone_clientpub fn (c &Client) clone_client() Client&Client 上的操作:拷贝客户端。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。Client — 该类型的值
responses_input_text_itempub fn responses_input_text_item(role string, text string) string执行 responses input text item 操作。
role stringtext stringstring — 结果字符串
responses_function_call_output_itempub fn responses_function_call_output_item(call_id string, output string) string执行 responses function call output item 操作。
call_id stringoutput stringstring — 结果字符串
responses_input_arraypub fn responses_input_array(items &[]string) string执行 responses input array 操作。
items &[]stringstring — 结果字符串
text_partpub fn text_part(text string) ContentPart执行 text part 操作。
text stringContentPart — 该类型的值
image_url_partpub fn image_url_part(url string, detail string) ContentPart执行 image url part 操作。
url stringdetail stringContentPart — 该类型的值
image_base64_partpub fn image_base64_part(mime_type string, b64_data string, detail string) ContentPart执行 image base64 part 操作。
mime_type stringb64_data stringdetail stringContentPart — 该类型的值
messagepub fn message(role string, content string) Message执行 message 处理。
role stringcontent stringMessage — 该类型的值
message_partspub fn message_parts(role string, parts []ContentPart) Message执行 message parts 操作。
role stringparts []ContentPartMessage — 该类型的值
tool_messagepub fn tool_message(tool_call_id string, content string) Message执行 tool message 操作。
tool_call_id stringcontent stringMessage — 该类型的值
json_schema_formatpub fn json_schema_format(name string, schema_json string, strict bool) string执行 json schema format 操作。
name stringschema_json stringstrict boolstring — 结果字符串
toolpub fn tool(name string, description string, parameters_json string) ToolDef执行 tool 处理。
name stringdescription stringparameters_json stringToolDef — 该类型的值
chat_requestpub fn chat_request(model string, messages []Message) ChatRequest执行 chat request 操作。
model stringmessages []MessageChatRequest — 该类型的值
new_completion_requestpub fn new_completion_request(model string, prompt string) CompletionRequest构造 completion request。
model stringprompt stringCompletionRequest — 该类型的值
embed_requestpub fn embed_request(model string, inputs []string) EmbedRequest执行 embed request 操作。
model stringinputs []stringEmbedRequest — 该类型的值
new_responses_requestpub fn new_responses_request(model string, input string) ResponsesRequest构造 responses request。
model stringinput stringResponsesRequest — 该类型的值
(&Client) list_modelspub fn (c &Client) list_models() []ModelInfo&Client 上的操作:执行 list models 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。[]ModelInfo — 切片结果
(&Client) chatpub fn (c &Client) chat(req &ChatRequest) ChatResponse在 &Client 上执行 chat 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ChatRequestChatResponse — 该类型的值
(&Client) chat_streampub fn (c &Client) chat_stream(req &ChatRequest, on_delta fn(StreamDelta) bool) ChatResponse&Client 上的操作:执行 chat stream 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ChatRequeston_delta fn(StreamDelta) boolChatResponse — 该类型的值
(&Client) chat_stream_chanpub fn (c &Client) chat_stream_chan(req &ChatRequest, delta_ch chan StreamDelta) ChatResponse&Client 上的操作:执行 chat stream chan 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ChatRequestdelta_ch chan StreamDeltaChatResponse — 该类型的值
(&Client) audio_transcriptionspub fn (c &Client) audio_transcriptions(model string, filename string, audio_bytes &string, language string, prompt string) string&Client 上的操作:执行 audio transcriptions 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。model stringfilename stringaudio_bytes &stringlanguage stringprompt stringstring — 结果字符串
(&Client) audio_speechpub fn (c &Client) audio_speech(model string, voice string, input string, response_format string) string&Client 上的操作:执行 audio speech 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。model stringvoice stringinput stringresponse_format stringstring — 结果字符串
(&Client) images_generatepub fn (c &Client) images_generate(model string, prompt string, size string, n int) []ImageGenResult&Client 上的操作:执行 images generate 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。model stringprompt stringsize stringn int[]ImageGenResult — 切片结果
(&Client) rerankpub fn (c &Client) rerank(model string, query string, documents &[]string, top_n int) []RerankResult在 &Client 上执行 rerank 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。model stringquery stringdocuments &[]stringtop_n int[]RerankResult — 切片结果
(&Client) embeddingspub fn (c &Client) embeddings(req &EmbedRequest) EmbedResponse在 &Client 上执行 embeddings 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &EmbedRequestEmbedResponse — 该类型的值
(&Client) completionspub fn (c &Client) completions(req &CompletionRequest) CompletionResponse在 &Client 上执行 completions 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &CompletionRequestCompletionResponse — 该类型的值
(&Client) responsespub fn (c &Client) responses(req &ResponsesRequest) ResponsesResponse在 &Client 上执行 responses 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ResponsesRequestResponsesResponse — 该类型的值
(&Client) responses_streampub fn (c &Client) responses_stream(req &ResponsesRequest, on_event fn(ResponsesStreamEvent) bool) ResponsesResponse&Client 上的操作:执行 responses stream 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ResponsesRequeston_event fn(ResponsesStreamEvent) boolResponsesResponse — 该类型的值
(&Client) responses_stream_chanpub fn (c &Client) responses_stream_chan(req &ResponsesRequest, ch chan ResponsesStreamEvent) ResponsesResponse&Client 上的操作:执行 responses stream chan 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。req &ResponsesRequestch chan ResponsesStreamEventResponsesResponse — 该类型的值
(&Client) upload_filepub fn (c &Client) upload_file(filename string, purpose string, content &string) FileObject&Client 上的操作:执行 upload file 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。filename stringpurpose stringcontent &stringFileObject — 该类型的值
(&Client) get_filepub fn (c &Client) get_file(file_id string) FileObject&Client 上的操作:读取 file。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。file_id stringFileObject — 该类型的值
(&Client) list_filespub fn (c &Client) list_files(purpose string) []FileObject&Client 上的操作:执行 list files 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。purpose string[]FileObject — 切片结果
(&Client) delete_filepub fn (c &Client) delete_file(file_id string) bool&Client 上的操作:删除文件。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。file_id stringbool — 是否成立
(&Client) get_file_contentpub fn (c &Client) get_file_content(file_id string) string&Client 上的操作:读取 file content。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。file_id stringstring — 结果字符串
(&Client) create_batchpub fn (c &Client) create_batch(input_file_id string, endpoint string, completion_window string, metadata_json string) BatchObject&Client 上的操作:执行 create batch 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。input_file_id stringendpoint stringcompletion_window stringmetadata_json stringBatchObject — 该类型的值
(&Client) get_batchpub fn (c &Client) get_batch(batch_id string) BatchObject&Client 上的操作:读取 batch。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。batch_id stringBatchObject — 该类型的值
(&Client) cancel_batchpub fn (c &Client) cancel_batch(batch_id string) BatchObject&Client 上的操作:执行 cancel batch 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。batch_id stringBatchObject — 该类型的值
(&Client) list_batchespub fn (c &Client) list_batches(limit int) []BatchObject&Client 上的操作:执行 list batches 操作。失败时抛出错误,调用方需处理或继续传播。
c &ClientClient 实例;以只读借用传入,调用方仍持有实例。limit int[]BatchObject — 切片结果