added google api
This commit is contained in:
parent
769b394dd7
commit
392e6b9957
@ -135,6 +135,8 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
|
|||||||
addMessageFunc = addGooseaiMessage
|
addMessageFunc = addGooseaiMessage
|
||||||
case "huggingface":
|
case "huggingface":
|
||||||
addMessageFunc = addHuggingfaceMessage
|
addMessageFunc = addHuggingfaceMessage
|
||||||
|
case "google":
|
||||||
|
addMessageFunc = addGoogleMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
var messageID edgedb.UUID
|
var messageID edgedb.UUID
|
||||||
|
@ -26,24 +26,29 @@ type GoogleChatCompletionRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GoogleChatCompletionResponse struct {
|
type GoogleChatCompletionResponse struct {
|
||||||
ID string `json:"id"`
|
Candidates []GoogleCandidate `json:"candidates"`
|
||||||
Object string `json:"object"`
|
UsageMetadata GoogleUsageMetadata `json:"usageMetadata"`
|
||||||
Created int64 `json:"created"`
|
|
||||||
Model string `json:"model"`
|
|
||||||
Usage GoogleUsage `json:"usage"`
|
|
||||||
Choices []GoogleChoice `json:"choices"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoogleUsage struct {
|
type GoogleCandidate struct {
|
||||||
PromptTokens int32 `json:"prompt_tokens"`
|
Content struct {
|
||||||
CompletionTokens int32 `json:"completion_tokens"`
|
Parts []GooglePart `json:"parts"`
|
||||||
TotalTokens int32 `json:"total_tokens"`
|
Role string `json:"role"`
|
||||||
|
} `json:"content"`
|
||||||
|
FinishReason string `json:"finishReason"`
|
||||||
|
Index int `json:"index"`
|
||||||
|
SafetyRatings []GoogleSafetyRating `json:"safetyRatings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoogleChoice struct {
|
type GoogleSafetyRating struct {
|
||||||
Message Message `json:"message"`
|
Category string `json:"category"`
|
||||||
FinishReason string `json:"finish_reason"`
|
Probability string `json:"probability"`
|
||||||
Index int `json:"index"`
|
}
|
||||||
|
|
||||||
|
type GoogleUsageMetadata struct {
|
||||||
|
PromptTokenCount int32 `json:"promptTokenCount"`
|
||||||
|
CandidatesTokenCount int32 `json:"candidatesTokenCount"`
|
||||||
|
TotalTokenCount int32 `json:"totalTokenCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func addGoogleMessage(llm LLM, selected bool) edgedb.UUID {
|
func addGoogleMessage(llm LLM, selected bool) edgedb.UUID {
|
||||||
@ -53,12 +58,12 @@ func addGoogleMessage(llm LLM, selected bool) edgedb.UUID {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error fetching user profile")
|
fmt.Println("Error fetching user profile")
|
||||||
panic(err)
|
panic(err)
|
||||||
} else if len(chatCompletion.Choices) == 0 {
|
} else if len(chatCompletion.Candidates) == 0 {
|
||||||
fmt.Println("No response from Google")
|
fmt.Println("No response from Google")
|
||||||
id := insertBotMessage("No response from Google", selected, llm.ID)
|
id := insertBotMessage("No response from Google", selected, llm.ID)
|
||||||
return id
|
return id
|
||||||
} else {
|
} else {
|
||||||
Content := chatCompletion.Choices[0].Message.Content
|
Content := chatCompletion.Candidates[0].Content.Parts[0].Text
|
||||||
id := insertBotMessage(Content, selected, llm.ID)
|
id := insertBotMessage(Content, selected, llm.ID)
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
@ -84,11 +89,13 @@ func TestGoogleKey(apiKey string) bool {
|
|||||||
|
|
||||||
jsonBody, err := json.Marshal(requestBody)
|
jsonBody, err := json.Marshal(requestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error marshalling JSON: ", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error creating request: ", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,27 +104,33 @@ func TestGoogleKey(apiKey string) bool {
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error sending request: ", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error reading response body: ", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var chatCompletionResponse GoogleChatCompletionResponse
|
var chatCompletionResponse GoogleChatCompletionResponse
|
||||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error unmarshaling JSON: ", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
if chatCompletionResponse.UsageMetadata.CandidatesTokenCount == 0 {
|
||||||
|
fmt.Println("No response from Google")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Response from Google: ", chatCompletionResponse)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func RequestGoogle(model string, messages []Message, temperature float64, context string) (OpenaiChatCompletionResponse, error) {
|
func RequestGoogle(model string, messages []Message, temperature float64, context string) (GoogleChatCompletionResponse, error) {
|
||||||
var apiKey string
|
var apiKey string
|
||||||
err := edgeClient.QuerySingle(edgeCtx, `
|
err := edgeClient.QuerySingle(edgeCtx, `
|
||||||
with
|
with
|
||||||
@ -127,48 +140,61 @@ func RequestGoogle(model string, messages []Message, temperature float64, contex
|
|||||||
} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
|
} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
|
||||||
)
|
)
|
||||||
select filtered_keys.key limit 1
|
select filtered_keys.key limit 1
|
||||||
`, &apiKey, "openai")
|
`, &apiKey, "google")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error getting OpenAI API key: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error getting Google API key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
url := "https://api.openai.com/v1/chat/completions"
|
url := "https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent?key=" + apiKey
|
||||||
|
|
||||||
requestBody := OpenaiChatCompletionRequest{
|
// Do an array of GoogleRequestMessage with the messages in a for loop
|
||||||
Model: model,
|
googleMessages := []GoogleRequestMessage{}
|
||||||
Messages: Message2RequestMessage(messages, context),
|
for _, message := range messages {
|
||||||
Temperature: temperature,
|
if message.Role == "user" {
|
||||||
|
googleMessages = append(googleMessages, GoogleRequestMessage{
|
||||||
|
Role: "user",
|
||||||
|
Parts: []GooglePart{GooglePart{Text: message.Content}},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
googleMessages = append(googleMessages, GoogleRequestMessage{
|
||||||
|
Role: "model",
|
||||||
|
Parts: []GooglePart{GooglePart{Text: message.Content}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBody := GoogleChatCompletionRequest{
|
||||||
|
Messages: googleMessages,
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBody, err := json.Marshal(requestBody)
|
jsonBody, err := json.Marshal(requestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error marshaling JSON: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error marshaling JSON: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error reading response body: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error reading response body: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
var chatCompletionResponse GoogleChatCompletionResponse
|
||||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error unmarshaling JSON: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error unmarshaling JSON: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var usedModelInfo ModelInfo
|
var usedModelInfo ModelInfo
|
||||||
@ -181,12 +207,12 @@ func RequestGoogle(model string, messages []Message, temperature float64, contex
|
|||||||
LIMIT 1
|
LIMIT 1
|
||||||
`, &usedModelInfo, model)
|
`, &usedModelInfo, model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return OpenaiChatCompletionResponse{}, fmt.Errorf("error getting model info: %w", err)
|
return GoogleChatCompletionResponse{}, fmt.Errorf("error getting model info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice
|
var inputCost float32 = float32(chatCompletionResponse.UsageMetadata.PromptTokenCount) * usedModelInfo.InputPrice
|
||||||
var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice
|
var outputCost float32 = float32(chatCompletionResponse.UsageMetadata.CandidatesTokenCount) * usedModelInfo.OutputPrice
|
||||||
addUsage(inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
addUsage(inputCost, outputCost, chatCompletionResponse.UsageMetadata.PromptTokenCount, chatCompletionResponse.UsageMetadata.CandidatesTokenCount, model)
|
||||||
|
|
||||||
return chatCompletionResponse, nil
|
return chatCompletionResponse, nil
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ html {
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
position: fixed;
|
position: fixed;
|
||||||
bottom: 10px;
|
bottom: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
right: 0;
|
right: 0;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</button-->
|
</button-->
|
||||||
<button disabled type="submit" class="send-button button is-primary is-small"
|
<button disabled type="submit" class="send-button button is-primary is-small"
|
||||||
hx-post="/generatePlaceholder" hx-swap="beforeend settle:200ms" hx-target="#chat-messages"
|
hx-post="/generatePlaceholder" hx-swap="beforeend" hx-target="#chat-messages"
|
||||||
id="chat-input-send-btn" class="chat-input" hx-include="[name='message']"
|
id="chat-input-send-btn" class="chat-input" hx-include="[name='message']"
|
||||||
hx-vals="js:{selectedLLMIds: getSelectedModelsIDs()}" onclick="clearTextArea()">
|
hx-vals="js:{selectedLLMIds: getSelectedModelsIDs()}" onclick="clearTextArea()">
|
||||||
<span class="icon">
|
<span class="icon">
|
||||||
@ -43,7 +43,7 @@
|
|||||||
<script>
|
<script>
|
||||||
var textareaControl = document.getElementById('textarea-control');
|
var textareaControl = document.getElementById('textarea-control');
|
||||||
|
|
||||||
// Every 0.1s check if the text area have htmx-request class, if yes, add the class is-loading
|
// Every 0.01s check if the text area have htmx-request class, if yes, add the class is-loading
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
if (textareaControl.classList.contains('htmx-request')) {
|
if (textareaControl.classList.contains('htmx-request')) {
|
||||||
textareaControl.classList.add('is-loading');
|
textareaControl.classList.add('is-loading');
|
||||||
@ -52,6 +52,22 @@
|
|||||||
}
|
}
|
||||||
}, 10);
|
}, 10);
|
||||||
|
|
||||||
|
setInterval(function () {
|
||||||
|
if (window.innerWidth < 450) {
|
||||||
|
var elements = document.getElementsByClassName('message-icon');
|
||||||
|
for (var i = 0; i < elements.length; i++) {
|
||||||
|
elements[i].classList.remove('is-48x48');
|
||||||
|
elements[i].classList.add('is-32x32');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var elements = document.getElementsByClassName('message-icon');
|
||||||
|
for (var i = 0; i < elements.length; i++) {
|
||||||
|
elements[i].classList.remove('is-32x32');
|
||||||
|
elements[i].classList.add('is-48x48');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
|
||||||
document.getElementById('chat-input-textarea').addEventListener('focus', function () {
|
document.getElementById('chat-input-textarea').addEventListener('focus', function () {
|
||||||
var buttons = document.getElementsByClassName('to-reduce-opacity');
|
var buttons = document.getElementsByClassName('to-reduce-opacity');
|
||||||
for (var i = 0; i < buttons.length; i++) {
|
for (var i = 0; i < buttons.length; i++) {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
|
||||||
<title>JADE</title>
|
<title>JADE</title>
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css">
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css">
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
|
||||||
|
@ -25,7 +25,8 @@
|
|||||||
<div class="column" id="content-column">
|
<div class="column" id="content-column">
|
||||||
{% if not IsPlaceholder %}
|
{% if not IsPlaceholder %}
|
||||||
<div class="{% if not notFlex%} is-flex {% endif %} is-align-items-start">
|
<div class="{% if not notFlex%} is-flex {% endif %} is-align-items-start">
|
||||||
<div class="message-content" id="content-{{ ConversationAreaId }}">
|
<div class="message-content" style="width: 100%; overflow-y: hidden;"
|
||||||
|
id="content-{{ ConversationAreaId }}">
|
||||||
{% for message in Messages %}
|
{% for message in Messages %}
|
||||||
{% if not message.Hidden %}
|
{% if not message.Hidden %}
|
||||||
<div class="message-header">
|
<div class="message-header">
|
||||||
@ -34,7 +35,7 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="message-body">
|
<div class="message-body">
|
||||||
<div class="content" style="overflow-x: auto;">
|
<div class="content" style="overflow-x: auto; width: 100%;">
|
||||||
{{ message.Content | safe }}
|
{{ message.Content | safe }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
<div class="message-user mt-3" id="msg-{{ ID }}">
|
<div class="message-user mt-3" id="msg-{{ ID }}">
|
||||||
<div class="columns is-mobile">
|
<div class="columns is-mobile">
|
||||||
<div class="column is-narrow" id="icon-column">
|
<div class="column is-narrow" id="icon-column">
|
||||||
<figure class="image is-48x48" style="flex-shrink: 0;">
|
<figure class="image is-48x48 message-icon" style="flex-shrink: 0;">
|
||||||
<img src="icons/bouvai2.png" alt="User Image">
|
<img src="icons/bouvai2.png" alt="User Image">
|
||||||
</figure>
|
</figure>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="column" id="content-column">
|
<div class="column" id="content-column">
|
||||||
<div class="is-flex is-align-items-start">
|
<div class="is-flex is-align-items-start">
|
||||||
<div class="message-content">
|
<div class="message-content" style="width: 100%; overflow-y: hidden;">
|
||||||
<div class="message-header">
|
<div class="message-header">
|
||||||
<p>
|
<p>
|
||||||
You
|
You
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="message-body">
|
<div class="message-body">
|
||||||
<div class="content" style="overflow-x: auto;" id="content-{{ ID }}">
|
<div class="content" style="overflow-x: auto; width: 100%;" id="content-{{ ID }}">
|
||||||
{{ Content | safe }}
|
{{ Content | safe }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -136,13 +136,11 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
document.getElementById('create-model-button').addEventListener('click', function () {
|
document.getElementById('create-model-button').addEventListener('click', function () {
|
||||||
console.log('create model');
|
|
||||||
document.getElementById('models-list').classList.add('is-hidden');
|
document.getElementById('models-list').classList.add('is-hidden');
|
||||||
document.getElementById('models-creation').classList.remove('is-hidden');
|
document.getElementById('models-creation').classList.remove('is-hidden');
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById('cancel-create-model-button').addEventListener('click', function () {
|
document.getElementById('cancel-create-model-button').addEventListener('click', function () {
|
||||||
console.log('cancel create model');
|
|
||||||
document.getElementById('models-list').classList.remove('is-hidden');
|
document.getElementById('models-list').classList.remove('is-hidden');
|
||||||
document.getElementById('models-creation').classList.add('is-hidden');
|
document.getElementById('models-creation').classList.add('is-hidden');
|
||||||
});
|
});
|
||||||
|
@ -55,7 +55,7 @@
|
|||||||
id="gemini-field">
|
id="gemini-field">
|
||||||
<p class="control has-icons-left is-expanded">
|
<p class="control has-icons-left is-expanded">
|
||||||
<input class="input is-small {% if GoogleExists %}is-success{% endif %}" type="text"
|
<input class="input is-small {% if GoogleExists %}is-success{% endif %}" type="text"
|
||||||
disabled placeholder="Google API key" name="google_key" autocomplete="off">
|
placeholder="Google API key" name="google_key" autocomplete="off">
|
||||||
<span class="icon is-small is-left">
|
<span class="icon is-small is-left">
|
||||||
<i class="fas fa-lock"></i>
|
<i class="fas fa-lock"></i>
|
||||||
</span>
|
</span>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user