-
Notifications
You must be signed in to change notification settings - Fork 12
/
openai_service.go
126 lines (107 loc) · 3.94 KB
/
openai_service.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package service
import (
"encoding/json"
"fmt"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-gmail/config"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-gmail/repository"
postgresEntity "github.com/openline-ai/openline-customer-os/packages/server/customer-os-postgres-repository/entity"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
"time"
)
type openAiService struct {
cfg *config.Config
repositories *repository.Repositories
}
type OpenAiService interface {
AskForOrganizationNameByDomain(tenant, elementId, domain string) (string, error)
}
func (s *openAiService) AskForOrganizationNameByDomain(tenant, elementId, domain string) (string, error) {
result, err := s.queryOpenAi(tenant, elementId, domain)
if err != nil {
logrus.Errorf("failed to query open ai: %v", err)
return "", err
}
choices := result["choices"].([]interface{})
if len(choices) > 0 {
if choices[0].(map[string]interface{})["finish_reason"] == "length" {
logrus.Errorf("not enough token to generate ai classification: %v", err)
return "", err
}
return choices[0].(map[string]interface{})["message"].(map[string]interface{})["content"].(string), nil
}
return "", nil
}
func (s *openAiService) queryOpenAi(tenant, elementId, domain string) (map[string]interface{}, error) {
prompt := "What name would have the organization that has this domain: " + domain + "\nI want a simple response in a single line with no other words ar analysis."
requestData := map[string]interface{}{}
requestData["model"] = "gpt-3.5-turbo-16k"
requestData["prompt"] = prompt
requestData["maxTokensToSample"] = 1024
requestBody, _ := json.Marshal(requestData)
request, _ := http.NewRequest("POST", s.cfg.Ai.ApiPath+"/ask-openai", strings.NewReader(string(requestBody)))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Openline-API-KEY", s.cfg.Ai.ApiKey)
nodeLabel := "RawEmail"
log := postgresEntity.AiPromptLog{
CreatedAt: time.Time{},
AppSource: "sync-gmail",
Provider: "anthropic",
Model: "claude-2",
PromptType: "ORGANIZATION_NAME_BY_DOMAIN",
Tenant: &tenant,
NodeId: &elementId,
NodeLabel: &nodeLabel,
Prompt: prompt,
}
storeLogId, errLog := s.repositories.PostgresRepositories.AiPromptLogRepository.Store(log)
if errLog != nil {
fmt.Println("Error:", errLog)
}
client := &http.Client{}
response, err := client.Do(request)
if response == nil || response.Body == nil {
s.repositories.PostgresRepositories.AiPromptLogRepository.UpdateError(storeLogId, "no response body")
fmt.Println("Error making the API request: no response body")
return nil, err
}
//store response
bodyBytes, errBody := ioutil.ReadAll(response.Body)
if errBody != nil {
fmt.Println("Error reading response body:", err)
return nil, err
}
bodyString := string(bodyBytes)
err = s.repositories.PostgresRepositories.AiPromptLogRepository.UpdateResponse(storeLogId, bodyString)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
if err != nil {
s.repositories.PostgresRepositories.AiPromptLogRepository.UpdateError(storeLogId, err.Error())
fmt.Println("Error making the API request:", err)
return nil, err
}
if response.StatusCode != 200 {
s.repositories.PostgresRepositories.AiPromptLogRepository.UpdateError(storeLogId, bodyString)
fmt.Println("Error making the API request:", response.Status)
return nil, fmt.Errorf("error making the API request: %s", response.Status)
}
defer response.Body.Close()
var result map[string]interface{}
err = json.Unmarshal(bodyBytes, &result)
if err != nil {
s.repositories.PostgresRepositories.AiPromptLogRepository.UpdateError(storeLogId, err.Error())
fmt.Println("Error parsing the API response:", err)
return nil, err
}
return result, nil
}
func NewOpenAiService(cfg *config.Config, repositories *repository.Repositories) OpenAiService {
return &openAiService{
cfg: cfg,
repositories: repositories,
}
}