forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
62 lines (54 loc) · 1.58 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gogpt
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
/*
- SearchRequest represents a request structure for search API.
- Info (*):
- 1*) You should specify either 'documents' or a 'file', but not both.
- 2*) This flag only takes effect when file is set.
*/
type SearchRequest struct {
Query string `json:"query"`
Documents []string `json:"documents"` // 1*
FileID string `json:"file,omitempty"` // 1*
MaxRerank int `json:"max_rerank,omitempty"` // 2*
ReturnMetadata bool `json:"return_metadata,omitempty"`
User string `json:"user,omitempty"`
}
// SearchResult represents single result from search API.
type SearchResult struct {
Document int `json:"document"`
Object string `json:"object"`
Score float32 `json:"score"`
Metadata string `json:"metadata"` // 2*
}
// SearchResponse represents a response structure for search API.
type SearchResponse struct {
SearchResults []SearchResult `json:"data"`
Object string `json:"object"`
}
// Search — perform a semantic search api call over a list of documents.
func (c *Client) Search(
ctx context.Context,
engineID string,
request SearchRequest,
) (response SearchResponse, err error) {
var reqBytes []byte
reqBytes, err = json.Marshal(request)
if err != nil {
return
}
urlSuffix := fmt.Sprintf("/engines/%s/search", engineID)
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}
req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}