openai api rs
v5.2.3
OpenAI API 클라이언트 Rust Library는 Rust Applications에서 OpenAI API에 편리하게 액세스 할 수 있습니다.
Docs.rs를 확인하십시오.
cargo.toml
[ dependencies ]
openai-api-rs = " 5.2.3 "
라이브러리는 웹 사이트에서 사용할 수있는 계정의 비밀 키로 구성해야합니다. 환경 변수로 설정하는 것이 좋습니다. 다음은 환경 변수에서로드 된 API 키로 라이브러리를 초기화하고 완료를 만드는 예입니다.
$ export OPENAI_API_KEY=sk-xxxxxxx
let api_key = env :: var ( "OPENAI_API_KEY" ) . unwrap ( ) . to_string ( ) ;
let client = OpenAIClient :: builder ( ) . with_api_key ( api_key ) . build ( ) ? ;
let req = ChatCompletionRequest :: new (
GPT4_O . to_string ( ) ,
vec ! [ chat_completion:: ChatCompletionMessage {
role: chat_completion:: MessageRole ::user,
content: chat_completion:: Content :: Text ( String ::from ( "What is bitcoin?" ) ) ,
name: None ,
tool_calls: None ,
tool_call_id: None ,
} ] ,
) ;
let result = client . chat_completion ( req ) ? ;
println ! ( "Content: {:?}" , result.choices [ 0 ] .message.content ) ;
$ export OPENAI_API_BASE=https://api.openai.com/v1
use openai_api_rs :: v1 :: api :: OpenAIClient ;
use openai_api_rs :: v1 :: chat_completion :: { self , ChatCompletionRequest } ;
use openai_api_rs :: v1 :: common :: GPT4_O ;
use std :: env ;
# [ tokio :: main ]
async fn main ( ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
let api_key = env :: var ( "OPENAI_API_KEY" ) . unwrap ( ) . to_string ( ) ;
let client = OpenAIClient :: builder ( ) . with_api_key ( api_key ) . build ( ) ? ;
let req = ChatCompletionRequest :: new (
GPT4_O . to_string ( ) ,
vec ! [ chat_completion:: ChatCompletionMessage {
role: chat_completion:: MessageRole ::user,
content: chat_completion:: Content :: Text ( String ::from ( "What is bitcoin?" ) ) ,
name: None ,
tool_calls: None ,
tool_call_id: None ,
} ] ,
) ;
let result = client . chat_completion ( req ) . await ? ;
println ! ( "Content: {:?}" , result.choices [ 0 ] .message.content ) ;
println ! ( "Response Headers: {:?}" , result.headers ) ;
Ok ( ( ) )
}
더 많은 예 : 예
사용 가능한 모든 기능의 예는 전체 API 문서를 확인하십시오.
이 프로젝트는 MIT 라이센스에 따라 라이센스가 부여됩니다.