177 lines
3.6 KiB
Go
177 lines
3.6 KiB
Go
package openrouter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
var Orclient OpenRouterClient
|
|
|
|
type OpenRouterClient struct {
|
|
APIKey string
|
|
Model string
|
|
}
|
|
type OpenRouterRequest struct {
|
|
Model string `json:"model"`
|
|
Messages []struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
} `json:"messages"`
|
|
}
|
|
|
|
type OpenRouterResponse struct {
|
|
Choices []struct {
|
|
Message struct {
|
|
Content string `json:"content"`
|
|
} `json:"message"`
|
|
} `json:"choices"`
|
|
}
|
|
|
|
func AskOpenRouter(prompt string) (string, error) {
|
|
url := "https://openrouter.ai/api/v1/chat/completions"
|
|
apiKey := Orclient.APIKey
|
|
model := Orclient.Model
|
|
reqBody := OpenRouterRequest{
|
|
Model: model,
|
|
}
|
|
|
|
reqBody.Messages = append(reqBody.Messages, struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
}{
|
|
Role: "user",
|
|
Content: prompt,
|
|
})
|
|
|
|
jsonData, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("HTTP-Referer", "https://yourapp.com") // optional but recommended
|
|
req.Header.Set("X-Title", "My App") // optional
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
return "", fmt.Errorf("error: %s", string(body))
|
|
}
|
|
|
|
var result OpenRouterResponse
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if len(result.Choices) == 0 {
|
|
return "", fmt.Errorf("no response from model")
|
|
}
|
|
|
|
return result.Choices[0].Message.Content, nil
|
|
}
|
|
|
|
func AskOpenRouterWithLocalImage(prompt, imagePath string) (string, error) {
|
|
url := "https://openrouter.ai/api/v1/chat/completions"
|
|
apiKey := Orclient.APIKey
|
|
model := Orclient.Model
|
|
// Read image file
|
|
imageBytes, err := os.ReadFile(imagePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Detect mime type (optional but good practice)
|
|
mimeType := http.DetectContentType(imageBytes)
|
|
|
|
// Encode to base64
|
|
base64Image := base64.StdEncoding.EncodeToString(imageBytes)
|
|
|
|
imageData := fmt.Sprintf("data:%s;base64,%s", mimeType, base64Image)
|
|
|
|
payload := map[string]interface{}{
|
|
"model": model,
|
|
"messages": []map[string]interface{}{
|
|
{
|
|
"role": "user",
|
|
"content": []map[string]interface{}{
|
|
{
|
|
"type": "text",
|
|
"text": prompt,
|
|
},
|
|
{
|
|
"type": "image_url",
|
|
"image_url": map[string]string{
|
|
"url": imageData,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
jsonData, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("HTTP-Referer", "https://yourapp.com")
|
|
req.Header.Set("X-Title", "My App")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
return "", fmt.Errorf("error: %s", string(body))
|
|
}
|
|
|
|
var result OpenRouterResponse
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if len(result.Choices) == 0 {
|
|
return "", fmt.Errorf("no response")
|
|
}
|
|
|
|
return result.Choices[0].Message.Content, nil
|
|
}
|