mirror of
https://github.com/fumiama/deepinfra.git
synced 2026-06-09 12:30:31 +08:00
init: add codes
This commit is contained in:
37
api.go
Normal file
37
api.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package deepinfra
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
APIDeepInfra = "https://api.deepinfra.com/v1/openai/chat/completions"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
api string // api to call
|
||||
key string // key in Authorization: Bearer
|
||||
}
|
||||
|
||||
func NewAPI(api, key string) API {
|
||||
return API{api: api, key: key}
|
||||
}
|
||||
|
||||
func (api *API) Request(model Model) (string, error) {
|
||||
req, err := http.NewRequest("POST", api.api, model.Body())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+api.key)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
err = model.Parse(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return model.Output(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user