diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-06-19 21:59:53 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-06-19 21:59:53 -0400 |
| commit | 60264ef896ea876456ea3168882049405c3ed38d (patch) | |
| tree | ba1b981372b31af09d4e5cf292f707da28355663 /web | |
| parent | 390f313d38a39be9876176eed4b2a50c11657b1e (diff) | |
| download | x-60264ef896ea876456ea3168882049405c3ed38d.tar.xz x-60264ef896ea876456ea3168882049405c3ed38d.zip | |
cmd: add new command xatci
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'web')
| -rw-r--r-- | web/marginalia/marginalia.go | 95 | ||||
| -rw-r--r-- | web/nodeinfo/nodeinfo.go | 2 | ||||
| -rw-r--r-- | web/openai/chatgpt/chatgpt.go | 9 |
3 files changed, 100 insertions, 6 deletions
diff --git a/web/marginalia/marginalia.go b/web/marginalia/marginalia.go new file mode 100644 index 0000000..efa4e83 --- /dev/null +++ b/web/marginalia/marginalia.go @@ -0,0 +1,95 @@ +// Package marginalia implements the Marginalia search API. +// +// You need an API key to use this. See the Marginalia API docs for more information: https://www.marginalia.nu/marginalia-search/api/ +package marginalia + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + + "within.website/x/web" +) + +type Request struct { + Query string + Count *int + Index *int +} + +type Response struct { + License string `json:"license"` + Query string `json:"query"` + Results []Result `json:"results"` +} + +type Result struct { + URL string `json:"url"` + Title string `json:"title"` + Description string `json:"description"` + Quality float64 `json:"quality"` + Details [][]Detail `json:"details"` +} + +type Detail struct { + Keyword string `json:"keyword"` + Count int `json:"count"` + FlagsUnstableAPI []string `json:"flagsUnstableAPI"` +} + +type Client struct { + apiKey string + httpCli *http.Client +} + +func New(apiKey string, httpCli *http.Client) *Client { + if httpCli == nil { + httpCli = &http.Client{} + } + + return &Client{ + apiKey: apiKey, + httpCli: httpCli, + } +} + +func (c *Client) Search(ctx context.Context, req *Request) (*Response, error) { + u, err := url.Parse("https://api.marginalia.nu/") + if err != nil { + return nil, err + } + u.Path = "/" + c.apiKey + "/search/" + url.QueryEscape(req.Query) + q := u.Query() + if req.Count != nil { + q.Set("count", fmt.Sprint(*req.Count)) + } + if req.Index != nil { + q.Set("index", fmt.Sprint(*req.Index)) + } + u.RawQuery = q.Encode() + + r, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + + resp, err := c.httpCli.Do(r) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, web.NewError(http.StatusOK, resp) + } + + defer resp.Body.Close() + + var result Response + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, err + } + + return &result, nil +} diff --git a/web/nodeinfo/nodeinfo.go b/web/nodeinfo/nodeinfo.go index 7c77c0e..2ab34ce 100644 --- a/web/nodeinfo/nodeinfo.go +++ b/web/nodeinfo/nodeinfo.go @@ -140,7 +140,7 @@ func FetchWithClient(ctx context.Context, cli *http.Client, nodeURL string) (*No // Fetch uses the standard library HTTP client to fetch node information. func Fetch(ctx context.Context, nodeURL string) (*Node, error) { cli := &http.Client{ - Transport: useragent.Transport("github.com/Xe/x/web/nodeinfo", "https://within.website/.x.botinfo", http.DefaultTransport), + Transport: useragent.Transport("within.website/x/web/nodeinfo", "https://within.website/.x.botinfo", http.DefaultTransport), } return FetchWithClient(ctx, cli, nodeURL) } diff --git a/web/openai/chatgpt/chatgpt.go b/web/openai/chatgpt/chatgpt.go index aab4ce7..9fb2d09 100644 --- a/web/openai/chatgpt/chatgpt.go +++ b/web/openai/chatgpt/chatgpt.go @@ -13,10 +13,9 @@ import ( ) type Request struct { - Model string `json:"model"` - Messages []Message `json:"messages"` - Functions []Function `json:"functions,omitempty"` - FunctionCall string `json:"function_call"` + Model string `json:"model"` + Messages []Message `json:"messages"` + Functions []Function `json:"functions,omitempty"` } type Function struct { @@ -36,7 +35,7 @@ type Param struct { type Message struct { Role string `json:"role"` Content string `json:"content"` - FunctionCall *Funcall `json:"function_call"` + FunctionCall *Funcall `json:"function_call,omitempty"` } type Funcall struct { |
