initial commit
This commit is contained in:
113
internal/infrastructure/llm/openrouter.go
Normal file
113
internal/infrastructure/llm/openrouter.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/paramah/ai_devs4/s01e01/internal/domain"
|
||||
)
|
||||
|
||||
// OpenRouterProvider implements domain.LLMProvider for OpenRouter API
|
||||
type OpenRouterProvider struct {
|
||||
apiKey string
|
||||
model string
|
||||
baseURL string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// NewOpenRouterProvider creates a new OpenRouter provider
|
||||
func NewOpenRouterProvider(apiKey, model string) *OpenRouterProvider {
|
||||
return &OpenRouterProvider{
|
||||
apiKey: apiKey,
|
||||
model: model,
|
||||
baseURL: "https://openrouter.ai/api/v1/chat/completions",
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
type openRouterRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []map[string]interface{} `json:"messages"`
|
||||
ResponseFormat *responseFormat `json:"response_format,omitempty"`
|
||||
}
|
||||
|
||||
type responseFormat struct {
|
||||
Type string `json:"type"`
|
||||
JSONSchema interface{} `json:"json_schema,omitempty"`
|
||||
}
|
||||
|
||||
type openRouterResponse struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// Complete sends a request to OpenRouter API
|
||||
func (p *OpenRouterProvider) Complete(ctx context.Context, request domain.LLMRequest) (*domain.LLMResponse, error) {
|
||||
reqBody := openRouterRequest{
|
||||
Model: p.model,
|
||||
Messages: []map[string]interface{}{
|
||||
{
|
||||
"role": "user",
|
||||
"content": request.Prompt,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if request.Schema != nil {
|
||||
reqBody.ResponseFormat = &responseFormat{
|
||||
Type: "json_schema",
|
||||
JSONSchema: request.Schema,
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+p.apiKey)
|
||||
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sending request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading response: %w", err)
|
||||
}
|
||||
|
||||
var apiResp openRouterResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
return nil, fmt.Errorf("unmarshaling response: %w", err)
|
||||
}
|
||||
|
||||
if apiResp.Error != nil {
|
||||
return nil, fmt.Errorf("API error: %s", apiResp.Error.Message)
|
||||
}
|
||||
|
||||
if len(apiResp.Choices) == 0 {
|
||||
return nil, fmt.Errorf("no choices in response")
|
||||
}
|
||||
|
||||
return &domain.LLMResponse{
|
||||
Content: apiResp.Choices[0].Message.Content,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user