fixed anthro

This commit is contained in:
Adrien Bouvais 2024-05-17 12:29:39 +02:00
parent 17880f1d0f
commit fbeb1bc7d9
4 changed files with 13 additions and 16 deletions

View File

@ -24,7 +24,7 @@ func GeneratePlaceholderHandler(c *fiber.Ctx) error {
// Step 1 I create a User message and send it as output with a placeholder // Step 1 I create a User message and send it as output with a placeholder
// that will make a request to GenerateMultipleMessagesHandler when loading // that will make a request to GenerateMultipleMessagesHandler when loading
message := c.FormValue("message", "") message := c.FormValue("message", "")
selectedLLMIds := []string{"3cd15ca8-1433-11ef-9f22-93f2b78c78de"} // TODO Hanle in the UI selectedLLMIds := []string{"1e5a07c4-12fe-11ef-8da6-67d29b408c53"} // TODO Hanle in the UI
var selectedLLMs []LLM var selectedLLMs []LLM
var selectedLLM LLM var selectedLLM LLM

View File

@ -42,7 +42,6 @@ func addAnthropicMessage(llm LLM, selected bool) edgedb.UUID {
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
} else if len(chatCompletion.Content) == 0 { } else if len(chatCompletion.Content) == 0 {
fmt.Println(chatCompletion)
fmt.Println("No response from Anthropic") fmt.Println("No response from Anthropic")
id := insertBotMessage("No response from Anthropic", selected, llm.ID) id := insertBotMessage("No response from Anthropic", selected, llm.ID)
return id return id
@ -108,20 +107,22 @@ func TestAnthropicKey(apiKey string) bool {
} }
func RequestAnthropic(model string, messages []Message, maxTokens int, temperature float64) (AnthropicChatCompletionResponse, error) { func RequestAnthropic(model string, messages []Message, maxTokens int, temperature float64) (AnthropicChatCompletionResponse, error) {
var apiKey string var apiKey struct {
Key string `edgedb:"key"`
}
err := edgeClient.QuerySingle(edgeCtx, ` err := edgeClient.QuerySingle(edgeCtx, `
with SELECT Key {
filtered_keys := ( key
select Key { }
key FILTER .<keys[is Setting].<setting[is User] = global currentUser and .company.name = "anthropic"
} filter .company.name = <str>$0 LIMIT 1
)
select filtered_keys.key limit 1
`, &apiKey, "anthropic") `, &apiKey, "anthropic")
if err != nil { if err != nil {
return AnthropicChatCompletionResponse{}, fmt.Errorf("error getting Anthropic API key: %w", err) return AnthropicChatCompletionResponse{}, fmt.Errorf("error getting Anthropic API key: %w", err)
} }
fmt.Println("apiKey:", apiKey)
url := "https://api.anthropic.com/v1/messages" url := "https://api.anthropic.com/v1/messages"
requestBody := AnthropicChatCompletionRequest{ requestBody := AnthropicChatCompletionRequest{
@ -131,7 +132,7 @@ func RequestAnthropic(model string, messages []Message, maxTokens int, temperatu
Temperature: temperature, Temperature: temperature,
} }
fmt.Println(maxTokens) fmt.Println("Message2RequestMessage(messages):", Message2RequestMessage(messages))
jsonBody, err := json.Marshal(requestBody) jsonBody, err := json.Marshal(requestBody)
if err != nil { if err != nil {
@ -143,9 +144,9 @@ func RequestAnthropic(model string, messages []Message, maxTokens int, temperatu
return AnthropicChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err) return AnthropicChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err)
} }
req.Header.Set("x-api-key", apiKey.Key)
req.Header.Set("content-Type", "application/json") req.Header.Set("content-Type", "application/json")
req.Header.Set("anthropic-version", "2023-06-01") req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("x-api-key", apiKey)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@ -41,7 +41,6 @@ func addOpenaiMessage(llm LLM, selected bool) edgedb.UUID {
Messages := getAllSelectedMessages() Messages := getAllSelectedMessages()
chatCompletion, err := RequestOpenai(llm.Model.ModelID, Messages, float64(llm.Temperature)) chatCompletion, err := RequestOpenai(llm.Model.ModelID, Messages, float64(llm.Temperature))
fmt.Println(chatCompletion)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
} else if len(chatCompletion.Choices) == 0 { } else if len(chatCompletion.Choices) == 0 {
@ -124,8 +123,6 @@ func RequestOpenai(model string, messages []Message, temperature float64) (Opena
return OpenaiChatCompletionResponse{}, fmt.Errorf("error getting OpenAI API key: %w", err) return OpenaiChatCompletionResponse{}, fmt.Errorf("error getting OpenAI API key: %w", err)
} }
fmt.Println("Messages:", messages)
url := "https://api.openai.com/v1/chat/completions" url := "https://api.openai.com/v1/chat/completions"
requestBody := OpenaiChatCompletionRequest{ requestBody := OpenaiChatCompletionRequest{

View File

@ -29,7 +29,6 @@ var (
// Function to send events to all clients // Function to send events to all clients
func sendEvent(event, data string) { func sendEvent(event, data string) {
fmt.Println("Sending event: " + event)
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()