This commit is contained in:
2026-03-12 02:10:57 +01:00
parent a8e769c73d
commit 3c6dd52d8f
29 changed files with 3323 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/paramah/ai_devs4/s01e02/internal/domain"
)
// Client implements domain.APIClient
type Client struct {
locationsURL string
accessLevelURL string
httpClient *http.Client
}
// NewClient creates a new API client
func NewClient(locationsURL, accessLevelURL string) *Client {
return &Client{
locationsURL: locationsURL,
accessLevelURL: accessLevelURL,
httpClient: &http.Client{},
}
}
// GetLocation sends a location request and returns the raw JSON response
func (c *Client) GetLocation(ctx context.Context, req domain.LocationRequest) ([]byte, error) {
jsonData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.locationsURL, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(httpReq)
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)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
}
return body, nil
}
// GetAccessLevel sends an access level request and returns the raw JSON response
func (c *Client) GetAccessLevel(ctx context.Context, req domain.AccessLevelRequest) ([]byte, error) {
jsonData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.accessLevelURL, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(httpReq)
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)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
}
return body, nil
}

View File

@@ -0,0 +1,59 @@
package json
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/paramah/ai_devs4/s01e02/internal/domain"
)
// LocationRepository implements domain.LocationRepository
type LocationRepository struct {
baseURL string
client *http.Client
}
// NewLocationRepository creates a new location repository
func NewLocationRepository(baseURL string) *LocationRepository {
return &LocationRepository{
baseURL: baseURL,
client: &http.Client{},
}
}
// LoadLocations loads locations from the API
func (r *LocationRepository) LoadLocations(ctx context.Context, apiKey string) ([]domain.Location, error) {
url := fmt.Sprintf("%s/data/%s/findhim_locations.json", r.baseURL, apiKey)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
resp, err := r.client.Do(req)
if err != nil {
return nil, fmt.Errorf("fetching locations: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response: %w", err)
}
var plantsData domain.PowerPlantsData
if err := json.Unmarshal(body, &plantsData); err != nil {
return nil, fmt.Errorf("parsing JSON: %w", err)
}
locations := plantsData.ToLocations()
return locations, nil
}

View File

@@ -0,0 +1,33 @@
package json
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/paramah/ai_devs4/s01e02/internal/domain"
)
// Repository implements domain.PersonRepository
type Repository struct{}
// NewRepository creates a new JSON repository
func NewRepository() *Repository {
return &Repository{}
}
// LoadPersons loads persons from a JSON file
func (r *Repository) LoadPersons(ctx context.Context, filePath string) ([]domain.Person, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("reading file: %w", err)
}
var persons []domain.Person
if err := json.Unmarshal(data, &persons); err != nil {
return nil, fmt.Errorf("parsing JSON: %w", err)
}
return persons, nil
}

View File

@@ -0,0 +1,123 @@
package llm
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/paramah/ai_devs4/s01e02/internal/domain"
)
// LMStudioProvider implements domain.LLMProvider for local LM Studio
type LMStudioProvider struct {
baseURL string
model string
client *http.Client
}
// NewLMStudioProvider creates a new LM Studio provider
func NewLMStudioProvider(baseURL, model string) *LMStudioProvider {
return &LMStudioProvider{
baseURL: baseURL,
model: model,
client: &http.Client{},
}
}
type lmStudioRequest struct {
Model string `json:"model"`
Messages []domain.LLMMessage `json:"messages"`
Tools []domain.Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
}
type lmStudioResponse struct {
Choices []struct {
Message domain.LLMMessage `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Error json.RawMessage `json:"error,omitempty"`
}
// Chat sends a chat request with function calling support
func (p *LMStudioProvider) Chat(ctx context.Context, request domain.LLMRequest) (*domain.LLMResponse, error) {
reqBody := lmStudioRequest{
Model: p.model,
Messages: request.Messages,
Tools: request.Tools,
Temperature: request.Temperature,
}
if request.ToolChoice != "" {
if request.ToolChoice == "auto" {
reqBody.ToolChoice = "auto"
} else {
reqBody.ToolChoice = map[string]interface{}{
"type": "function",
"function": map[string]string{
"name": request.ToolChoice,
},
}
}
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
url := p.baseURL + "/v1/chat/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
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)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
}
var apiResp lmStudioResponse
if err := json.Unmarshal(body, &apiResp); err != nil {
return nil, fmt.Errorf("unmarshaling response: %w\nResponse body: %s", err, string(body))
}
if len(apiResp.Error) > 0 {
var errStr string
if err := json.Unmarshal(apiResp.Error, &errStr); err == nil {
return nil, fmt.Errorf("API error: %s", errStr)
}
var errObj struct {
Message string `json:"message"`
}
if err := json.Unmarshal(apiResp.Error, &errObj); err == nil {
return nil, fmt.Errorf("API error: %s", errObj.Message)
}
return nil, fmt.Errorf("API error: %s", string(apiResp.Error))
}
if len(apiResp.Choices) == 0 {
return nil, fmt.Errorf("no choices in response. Response body: %s", string(body))
}
return &domain.LLMResponse{
Message: apiResp.Choices[0].Message,
FinishReason: apiResp.Choices[0].FinishReason,
}, nil
}

View File

@@ -0,0 +1,113 @@
package llm
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/paramah/ai_devs4/s01e02/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 []domain.LLMMessage `json:"messages"`
Tools []domain.Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
}
type openRouterResponse struct {
Choices []struct {
Message domain.LLMMessage `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
// Chat sends a chat request with function calling support
func (p *OpenRouterProvider) Chat(ctx context.Context, request domain.LLMRequest) (*domain.LLMResponse, error) {
reqBody := openRouterRequest{
Model: p.model,
Messages: request.Messages,
Tools: request.Tools,
Temperature: request.Temperature,
}
if request.ToolChoice != "" {
if request.ToolChoice == "auto" {
reqBody.ToolChoice = "auto"
} else {
reqBody.ToolChoice = map[string]interface{}{
"type": "function",
"function": map[string]string{
"name": request.ToolChoice,
},
}
}
}
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{
Message: apiResp.Choices[0].Message,
FinishReason: apiResp.Choices[0].FinishReason,
}, nil
}