initial commit
This commit is contained in:
116
internal/infrastructure/csv/repository.go
Normal file
116
internal/infrastructure/csv/repository.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package csv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/paramah/ai_devs4/s01e01/internal/domain"
|
||||
)
|
||||
|
||||
// Repository implements domain.PersonRepository
|
||||
type Repository struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// NewRepository creates a new CSV repository
|
||||
func NewRepository() *Repository {
|
||||
return &Repository{
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
// FetchPeople fetches people from a CSV file at the given URL
|
||||
func (r *Repository) FetchPeople(ctx context.Context, url string) ([]domain.Person, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, 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 CSV: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
reader := csv.NewReader(resp.Body)
|
||||
|
||||
// Read header
|
||||
header, err := reader.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading header: %w", err)
|
||||
}
|
||||
|
||||
// Parse header to get column indices
|
||||
indices := make(map[string]int)
|
||||
for i, col := range header {
|
||||
indices[strings.TrimSpace(col)] = i
|
||||
}
|
||||
|
||||
var people []domain.Person
|
||||
for {
|
||||
record, err := reader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading record: %w", err)
|
||||
}
|
||||
|
||||
person, err := r.parsePerson(record, indices)
|
||||
if err != nil {
|
||||
// Skip invalid records
|
||||
continue
|
||||
}
|
||||
|
||||
people = append(people, person)
|
||||
}
|
||||
|
||||
return people, nil
|
||||
}
|
||||
|
||||
func (r *Repository) parsePerson(record []string, indices map[string]int) (domain.Person, error) {
|
||||
var person domain.Person
|
||||
|
||||
if idx, ok := indices["name"]; ok && idx < len(record) {
|
||||
person.Name = strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
if idx, ok := indices["surname"]; ok && idx < len(record) {
|
||||
person.Surname = strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
if idx, ok := indices["gender"]; ok && idx < len(record) {
|
||||
person.Gender = strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
// Parse birthDate (format: YYYY-MM-DD) to extract year
|
||||
if idx, ok := indices["birthDate"]; ok && idx < len(record) {
|
||||
birthDate := strings.TrimSpace(record[idx])
|
||||
if len(birthDate) >= 4 {
|
||||
if year, err := strconv.Atoi(birthDate[:4]); err == nil {
|
||||
person.Born = year
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use birthPlace as city
|
||||
if idx, ok := indices["birthPlace"]; ok && idx < len(record) {
|
||||
person.City = strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
// Read job description
|
||||
if idx, ok := indices["job"]; ok && idx < len(record) {
|
||||
person.Job = strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
return person, nil
|
||||
}
|
||||
130
internal/infrastructure/llm/lmstudio.go
Normal file
130
internal/infrastructure/llm/lmstudio.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/paramah/ai_devs4/s01e01/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 []map[string]interface{} `json:"messages"`
|
||||
ResponseFormat *lmResponseFormat `json:"response_format,omitempty"`
|
||||
Temperature float64 `json:"temperature,omitempty"`
|
||||
}
|
||||
|
||||
type lmResponseFormat struct {
|
||||
Type string `json:"type"`
|
||||
JSONSchema interface{} `json:"json_schema,omitempty"`
|
||||
}
|
||||
|
||||
type lmStudioResponse struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error json.RawMessage `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// Complete sends a request to LM Studio local server
|
||||
func (p *LMStudioProvider) Complete(ctx context.Context, request domain.LLMRequest) (*domain.LLMResponse, error) {
|
||||
reqBody := lmStudioRequest{
|
||||
Model: p.model,
|
||||
Messages: []map[string]interface{}{
|
||||
{
|
||||
"role": "user",
|
||||
"content": request.Prompt,
|
||||
},
|
||||
},
|
||||
Temperature: 0.7,
|
||||
}
|
||||
|
||||
if request.Schema != nil {
|
||||
reqBody.ResponseFormat = &lmResponseFormat{
|
||||
Type: "json_schema",
|
||||
JSONSchema: request.Schema,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Check HTTP status
|
||||
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))
|
||||
}
|
||||
|
||||
// Check for error in response
|
||||
if len(apiResp.Error) > 0 {
|
||||
// Try to parse as string
|
||||
var errStr string
|
||||
if err := json.Unmarshal(apiResp.Error, &errStr); err == nil {
|
||||
return nil, fmt.Errorf("API error: %s", errStr)
|
||||
}
|
||||
// Try to parse as object with message field
|
||||
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)
|
||||
}
|
||||
// Fallback to raw error
|
||||
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{
|
||||
Content: apiResp.Choices[0].Message.Content,
|
||||
}, nil
|
||||
}
|
||||
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