|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +// llmProxy implements the LlmRepository interface by acting as a proxy to the Python LLM service. |
| 11 | +type llmProxy struct { |
| 12 | + BaseURL string |
| 13 | + Client *http.Client |
| 14 | +} |
| 15 | + |
| 16 | +type LlmRepository interface { |
| 17 | + GenerateNotebook(ctx context.Context, body io.Reader) (*http.Response, error) |
| 18 | + ModifyNotebook(ctx context.Context, sessionID string, body io.Reader) (*http.Response, error) |
| 19 | + FixNotebook(ctx context.Context, sessionID string, body io.Reader) (*http.Response, error) |
| 20 | +} |
| 21 | + |
| 22 | +// NewLlmProxy creates a new llmProxy. |
| 23 | +func NewLlmProxy(baseURL string) LlmRepository { |
| 24 | + return &llmProxy{ |
| 25 | + BaseURL: baseURL, |
| 26 | + Client: &http.Client{}, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// GenerateNotebook proxies the request to the /generate endpoint of the LLM service. |
| 31 | +func (p *llmProxy) GenerateNotebook(ctx context.Context, body io.Reader) (*http.Response, error) { |
| 32 | + targetURL := fmt.Sprintf("%s/v1/generate", p.BaseURL) |
| 33 | + |
| 34 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, body) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("failed to create generate request: %w", err) |
| 37 | + } |
| 38 | + req.Header.Set("Content-Type", "application/json") |
| 39 | + req.Header.Set("Accept", "application/json") |
| 40 | + |
| 41 | + resp, err := p.Client.Do(req) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to call generate endpoint: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + return resp, nil |
| 47 | +} |
| 48 | + |
| 49 | +// ModifyNotebook proxies the request to the /sessions/{session_id}/modify endpoint. |
| 50 | +func (p *llmProxy) ModifyNotebook(ctx context.Context, sessionID string, body io.Reader) (*http.Response, error) { |
| 51 | + targetURL := fmt.Sprintf("%s/v1/sessions/%s/modify", p.BaseURL, sessionID) |
| 52 | + |
| 53 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, body) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to create modify request: %w", err) |
| 56 | + } |
| 57 | + req.Header.Set("Content-Type", "application/json") |
| 58 | + req.Header.Set("Accept", "application/json") |
| 59 | + |
| 60 | + resp, err := p.Client.Do(req) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to call modify endpoint: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + return resp, nil |
| 66 | +} |
| 67 | + |
| 68 | +// FixNotebook proxies the request to the /sessions/{session_id}/fix endpoint. |
| 69 | +func (p *llmProxy) FixNotebook(ctx context.Context, sessionID string, body io.Reader) (*http.Response, error) { |
| 70 | + targetURL := fmt.Sprintf("%s/v1/sessions/%s/fix", p.BaseURL, sessionID) |
| 71 | + |
| 72 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, body) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to create fix request: %w", err) |
| 75 | + } |
| 76 | + req.Header.Set("Content-Type", "application/json") |
| 77 | + req.Header.Set("Accept", "application/json") |
| 78 | + |
| 79 | + resp, err := p.Client.Do(req) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("failed to call fix endpoint: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + return resp, nil |
| 85 | +} |
0 commit comments