openaigo
1.0.0
api.openai.com 의 또 다른 API 클라이언트.
이 도서관은 공식적으로 OpenAI의 지원을받지 않은 커뮤니티에서 관리되어 있습니다.
package main
import (
"context"
"fmt"
"os"
"github.com/otiai10/openaigo"
)
func main () {
client := openaigo . NewClient ( os . Getenv ( "OPENAI_API_KEY" ))
request := openaigo. ChatRequest {
Model : "gpt-4o" ,
Messages : []openaigo. Message {
{ Role : "user" , Content : "Hello!" },
},
}
ctx := context . Background ()
response , err := client . Chat ( ctx , request )
fmt . Println ( response , err )
}시도하고 싶다면 아래 명령을 쳤다.
git clone https://github.com/otiai10/openaigo.git
cd openaigo
OPENAI_API_KEY=YourAPIKey go run ./testapp/main.go테스트 앱을 작업 예로 참조하십시오.
https://beta.openai.com/account/api-keys를 방문하면 무료로 시작하려면 자신만의 API 키를 만들 수 있습니다.
function_call 필요하십니까? request := openaigo. ChatRequest {
Messages : []openaigo. Message {
{ Role : "user" , Content : "How's the weather today in Tokyo?" },
},
Functions : []openaigo. Function {
{
Name : "get_weather" ,
Parameters : openaigo. Parameters {
Type : "object" ,
Properties : map [ string ] map [ string ] any {
"location" : { "type" : "string" },
"date" : { "type" : "string" , "description" : "ISO 8601 date string" },
},
Required : [] string { "location" },
},
}
},
} 속기를 원한다면 functioncall 사용하십시오.
import fc "github.com/otiai10/openaigo/functioncall"
request . Functions = fc. Funcs {
"get_weather" : { GetWeather , "Get weather of the location" , fc. Params {
{ "location" , "string" , "location of the weather" , true },
{ "date" , "string" , "ISO 8601 date string" , true },
}},
}테스트 앱을 작업 예로 참조하십시오.
stream 필요하십니까? client := openaigo . NewClient ( OPENAI_API_KEY )
request := openaigo. ChatRequest {
Stream : true ,
StreamCallback : func ( res ChatCompletionResponse , done bool , err error ) {
// Do what you want!
// You might need chan handling here.
// See test app how you can do it.
// https://github.com/otiai10/openaigo/search?q=chat_completion_stream
},
} client := openaigo . NewClient ( OPENAI_API_KEY )
// You can set whatever you want
transport := & http. Transport { Proxy : http . ProxyFromEnvironment }
client . HTTPClient = & http. Client { Transport : transport }
// Done!여기에 모든 문제를보고하거나 피드백을 환영합니다.