diff --git a/README.md b/README.md index 9a9639d..7ad0cd9 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,65 @@ ### Configuration -| Command line | Environment | Default | Description | -|--------------|-----------------|----------------|-------------------------------------------------------| -| address | UKEEPER_ADDRESS | all interfaces | web server listening address | -| port | UKEEPER_PORT | `8080` | web server port | -| mongo-uri | MONGO_URI | none | MongoDB connection string, _required_ | -| frontend-dir | FRONTEND_DIR | `/srv/web` | directory with frontend files | -| token | TOKEN | none | token for /content/v1/parser endpoint auth | -| mongo-delay | MONGO_DELAY | `0` | mongo initial delay | -| mongo-db | MONGO_DB | `ureadability` | mongo database name | -| creds | CREDS | none | credentials for protected calls (POST, DELETE /rules) | -| dbg | DEBUG | `false` | debug mode | +| Command line | Environment | Default | Description | +|----------------|-----------------|----------------|-------------------------------------------------------| +| --address | UKEEPER_ADDRESS | all interfaces | web server listening address | +| --port | UKEEPER_PORT | `8080` | web server port | +| --mongo-uri | MONGO_URI | none | MongoDB connection string, _required_ | +| --frontend-dir | FRONTEND_DIR | `/srv/web` | directory with frontend templates and static assets | +| --token | UKEEPER_TOKEN | none | token for /content/v1/parser endpoint auth | +| --mongo-delay | MONGO_DELAY | `0` | mongo initial delay | +| --mongo-db | MONGO_DB | `ureadability` | mongo database name | +| --creds | CREDS | none | credentials for protected calls (POST, DELETE /rules) | +| --dbg | DEBUG | `false` | debug mode | + +OpenAI Configuration: + +| Command line | Environment | Default | Description | +|------------------------------|----------------------------|---------------|----------------------------------------------------------------------| +| --openai.disable-summaries | OPENAI_DISABLE_SUMMARIES | `false` | disable summary generation with OpenAI | +| --openai.api-key | OPENAI_API_KEY | none | OpenAI API key for summary generation | +| --openai.model-type | OPENAI_MODEL_TYPE | `gpt-4o-mini` | OpenAI model name for summary generation (e.g., gpt-4o, gpt-4o-mini) | +| --openai.summary-prompt | OPENAI_SUMMARY_PROMPT | *see code* | custom prompt for summary generation | +| --openai.max-content-length | OPENAI_MAX_CONTENT_LENGTH | `10000` | maximum content length to send to OpenAI API | +| --openai.requests-per-minute | OPENAI_REQUESTS_PER_MINUTE | `10` | maximum number of OpenAI API requests per minute | +| --openai.cleanup-interval | OPENAI_CLEANUP_INTERVAL | `24h` | interval for cleaning up expired summaries | ### API - GET /api/content/v1/parser?token=secret&url=http://aa.com/blah - extract content (emulate Readability API parse call) + GET /api/content/v1/parser?token=secret&summary=true&url=http://aa.com/blah - extract content (emulate Readability API parse call), summary is optional and requires OpenAI key and token to be enabled POST /api/v1/extract {url: http://aa.com/blah} - extract content +### Article Summary Feature + +The application can generate concise summaries of article content using OpenAI's GPT models: + +1. **Configuration**: + - Set `--openai.api-key` to your OpenAI API key + - Summaries are enabled by default, use `--openai.disable-summaries` to disable this feature + - Optionally set `--openai.model-type` to specify which model to use (e.g., `gpt-4o`, `gpt-4o-mini`) + - Default is `gpt-4o-mini` if not specified + - A server token must be configured for security reasons + - Customize rate limiting with `--openai.requests-per-minute` (default: 10) + - Control content length with `--openai.max-content-length` (default: 10000 characters) + - Configure cleanup interval with `--openai.cleanup-interval` (default: 24h) + +2. **Usage**: + - Add `summary=true` parameter to the `/api/content/v1/parser` endpoint + - Example: `/api/content/v1/parser?token=secret&summary=true&url=http://example.com/article` + +3. **Features**: + - Summaries are cached in MongoDB to reduce API costs and improve performance + - The cache stores: + - Content hash (to identify articles) + - Summary text + - Model used for generation + - Creation and update timestamps + - Expiration time (defaults to 1 month) + - If the same content is requested again, the cached summary is returned + - The preview page automatically shows summaries when available + - Expired summaries are automatically cleaned up based on the configured interval + ## Development ### Running tests diff --git a/backend/datastore/mongo.go b/backend/datastore/mongo.go index 1d3ef5f..75ef9d6 100644 --- a/backend/datastore/mongo.go +++ b/backend/datastore/mongo.go @@ -39,7 +39,8 @@ func New(connectionURI, dbName string, delay time.Duration) (*MongoServer, error // Stores contains all DAO instances type Stores struct { - Rules RulesDAO + Rules RulesDAO + Summaries SummariesDAO } // GetStores initialize collections and make indexes @@ -50,8 +51,15 @@ func (m *MongoServer) GetStores() Stores { {Keys: bson.D{{Key: "domain", Value: 1}, {Key: "match_urls", Value: 1}}}, } + sIndexes := []mongo.IndexModel{ + {Keys: bson.D{{Key: "created_at", Value: 1}}}, + {Keys: bson.D{{Key: "model", Value: 1}}}, + {Keys: bson.D{{Key: "expires_at", Value: 1}}}, // index for cleaning up expired summaries + } + return Stores{ - Rules: RulesDAO{Collection: m.collection("rules", rIndexes)}, + Rules: RulesDAO{Collection: m.collection("rules", rIndexes)}, + Summaries: SummariesDAO{Collection: m.collection("summaries", sIndexes)}, } } diff --git a/backend/datastore/summaries.go b/backend/datastore/summaries.go new file mode 100644 index 0000000..54186d0 --- /dev/null +++ b/backend/datastore/summaries.go @@ -0,0 +1,109 @@ +// Package datastore provides mongo implementation for store to keep and access summaries +package datastore + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "fmt" + "time" + + log "github.com/go-pkgz/lgr" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +// Summary contains information about a cached summary +type Summary struct { + ID string `bson:"_id"` // SHA256 hash of the content + Content string `bson:"content"` // original content that was summarized (could be truncated for storage efficiency) + Summary string `bson:"summary"` // generated summary + Model string `bson:"model"` // openAI model used for summarization + CreatedAt time.Time `bson:"created_at"` + UpdatedAt time.Time `bson:"updated_at"` + ExpiresAt time.Time `bson:"expires_at"` // when this summary expires +} + +// SummariesDAO handles database operations for article summaries +type SummariesDAO struct { + Collection *mongo.Collection +} + +// Get returns summary by content hash +func (s SummariesDAO) Get(ctx context.Context, content string) (Summary, bool) { + contentHash := GenerateContentHash(content) + res := s.Collection.FindOne(ctx, bson.M{"_id": contentHash}) + if res.Err() != nil { + if res.Err() == mongo.ErrNoDocuments { + return Summary{}, false + } + log.Printf("[WARN] can't get summary for hash %s: %v", contentHash, res.Err()) + return Summary{}, false + } + + summary := Summary{} + if err := res.Decode(&summary); err != nil { + log.Printf("[WARN] can't decode summary document for hash %s: %v", contentHash, err) + return Summary{}, false + } + + return summary, true +} + +// Save creates or updates summary in the database +func (s SummariesDAO) Save(ctx context.Context, summary Summary) error { + if summary.ID == "" { + summary.ID = GenerateContentHash(summary.Content) + } + + if summary.CreatedAt.IsZero() { + summary.CreatedAt = time.Now() + } + summary.UpdatedAt = time.Now() + + // set default expiration of 1 month if not specified + if summary.ExpiresAt.IsZero() { + summary.ExpiresAt = time.Now().AddDate(0, 1, 0) + } + + opts := options.Update().SetUpsert(true) + _, err := s.Collection.UpdateOne( + ctx, + bson.M{"_id": summary.ID}, + bson.M{"$set": summary}, + opts, + ) + if err != nil { + return fmt.Errorf("failed to save summary: %w", err) + } + return nil +} + +// Delete removes summary from the database +func (s SummariesDAO) Delete(ctx context.Context, contentHash string) error { + _, err := s.Collection.DeleteOne(ctx, bson.M{"_id": contentHash}) + if err != nil { + return fmt.Errorf("failed to delete summary: %w", err) + } + return nil +} + +// CleanupExpired removes all summaries that have expired +func (s SummariesDAO) CleanupExpired(ctx context.Context) (int64, error) { + now := time.Now() + result, err := s.Collection.DeleteMany( + ctx, + bson.M{"expires_at": bson.M{"$lt": now}}, + ) + if err != nil { + return 0, fmt.Errorf("failed to cleanup expired summaries: %w", err) + } + return result.DeletedCount, nil +} + +// GenerateContentHash creates a hash for the content to use as an ID +func GenerateContentHash(content string) string { + hash := sha256.Sum256([]byte(content)) + return hex.EncodeToString(hash[:]) +} diff --git a/backend/datastore/summaries_test.go b/backend/datastore/summaries_test.go new file mode 100644 index 0000000..ca6bd31 --- /dev/null +++ b/backend/datastore/summaries_test.go @@ -0,0 +1,165 @@ +package datastore + +import ( + "context" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +func TestSummariesDAO_SaveAndGet(t *testing.T) { + if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok { + t.Skip("ENABLE_MONGO_TESTS env variable is not set") + } + + mdb, err := New("mongodb://localhost:27017", "test_ureadability", 0) + require.NoError(t, err) + + // create a unique collection for this test to avoid conflicts + collection := mdb.client.Database(mdb.dbName).Collection("summaries_test") + defer func() { + _ = collection.Drop(context.Background()) + }() + + // create an index on the expiresAt field + _, err = collection.Indexes().CreateOne(context.Background(), + mongo.IndexModel{ + Keys: bson.D{{"expires_at", 1}}, + }) + require.NoError(t, err) + + dao := SummariesDAO{Collection: collection} + + content := "This is a test article content. It should generate a unique hash." + summary := Summary{ + Content: content, + Summary: "This is a test summary of the article.", + Model: "gpt-4o-mini", + CreatedAt: time.Now(), + } + + // test saving a summary + err = dao.Save(context.Background(), summary) + require.NoError(t, err) + + // test getting the summary + foundSummary, found := dao.Get(context.Background(), content) + assert.True(t, found) + assert.Equal(t, summary.Summary, foundSummary.Summary) + assert.Equal(t, summary.Model, foundSummary.Model) + assert.NotEmpty(t, foundSummary.ID) + + // test getting a non-existent summary + _, found = dao.Get(context.Background(), "non-existent content") + assert.False(t, found) + + // test updating an existing summary + updatedSummary := Summary{ + ID: foundSummary.ID, + Content: content, + Summary: "This is an updated summary.", + Model: "gpt-4o-mini", + CreatedAt: foundSummary.CreatedAt, + } + + err = dao.Save(context.Background(), updatedSummary) + require.NoError(t, err) + + foundSummary, found = dao.Get(context.Background(), content) + assert.True(t, found) + assert.Equal(t, "This is an updated summary.", foundSummary.Summary) + assert.Equal(t, updatedSummary.CreatedAt, foundSummary.CreatedAt) + assert.NotEqual(t, updatedSummary.UpdatedAt, foundSummary.UpdatedAt) // UpdatedAt should be set by the DAO + + // test deleting a summary + err = dao.Delete(context.Background(), foundSummary.ID) + require.NoError(t, err) + + _, found = dao.Get(context.Background(), content) + assert.False(t, found) +} + +func TestGenerateContentHash(t *testing.T) { + content1 := "This is a test content." + content2 := "This is a different test content." + + hash1 := GenerateContentHash(content1) + hash2 := GenerateContentHash(content2) + + assert.NotEqual(t, hash1, hash2) + assert.Equal(t, hash1, GenerateContentHash(content1)) // same content should produce same hash + assert.Equal(t, 64, len(hash1)) // SHA-256 produces 64 character hex string +} + +func TestSummariesDAO_CleanupExpired(t *testing.T) { + if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok { + t.Skip("ENABLE_MONGO_TESTS env variable is not set") + } + + mdb, err := New("mongodb://localhost:27017", "test_ureadability", 0) + require.NoError(t, err) + + // create a unique collection for this test to avoid conflicts + collection := mdb.client.Database(mdb.dbName).Collection("summaries_expired_test") + defer func() { + _ = collection.Drop(context.Background()) + }() + + // create an index on the expiresAt field + _, err = collection.Indexes().CreateOne(context.Background(), + mongo.IndexModel{ + Keys: bson.D{{"expires_at", 1}}, + }) + require.NoError(t, err) + + dao := SummariesDAO{Collection: collection} + ctx := context.Background() + + // add expired summary + expiredSummary := Summary{ + Content: "This is an expired summary", + Summary: "Expired content", + Model: "gpt-4o-mini", + CreatedAt: time.Now().Add(-48 * time.Hour), + UpdatedAt: time.Now().Add(-48 * time.Hour), + ExpiresAt: time.Now().Add(-24 * time.Hour), // expired 24 hours ago + } + err = dao.Save(ctx, expiredSummary) + require.NoError(t, err) + + // add valid summary + validSummary := Summary{ + Content: "This is a valid summary", + Summary: "Valid content", + Model: "gpt-4o-mini", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + ExpiresAt: time.Now().Add(24 * time.Hour), // expires in 24 hours + } + err = dao.Save(ctx, validSummary) + require.NoError(t, err) + + // verify both summaries exist + _, foundExpired := dao.Get(ctx, expiredSummary.Content) + assert.True(t, foundExpired, "Expected to find expired summary before cleanup") + + _, foundValid := dao.Get(ctx, validSummary.Content) + assert.True(t, foundValid, "Expected to find valid summary before cleanup") + + // run cleanup + count, err := dao.CleanupExpired(ctx) + require.NoError(t, err) + assert.Equal(t, int64(1), count, "Expected to clean up exactly one record") + + // verify expired summary is gone but valid remains + _, foundExpired = dao.Get(ctx, expiredSummary.Content) + assert.False(t, foundExpired, "Expected expired summary to be deleted") + + _, foundValid = dao.Get(ctx, validSummary.Content) + assert.True(t, foundValid, "Expected valid summary to still exist") +} diff --git a/backend/extractor/openai_mock.go b/backend/extractor/openai_mock.go new file mode 100644 index 0000000..1ab184e --- /dev/null +++ b/backend/extractor/openai_mock.go @@ -0,0 +1,82 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package extractor + +import ( + "context" + "github.com/sashabaranov/go-openai" + "sync" +) + +// Ensure, that OpenAIClientMock does implement OpenAIClient. +// If this is not the case, regenerate this file with moq. +var _ OpenAIClient = &OpenAIClientMock{} + +// OpenAIClientMock is a mock implementation of OpenAIClient. +// +// func TestSomethingThatUsesOpenAIClient(t *testing.T) { +// +// // make and configure a mocked OpenAIClient +// mockedOpenAIClient := &OpenAIClientMock{ +// CreateChatCompletionFunc: func(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) { +// panic("mock out the CreateChatCompletion method") +// }, +// } +// +// // use mockedOpenAIClient in code that requires OpenAIClient +// // and then make assertions. +// +// } +type OpenAIClientMock struct { + // CreateChatCompletionFunc mocks the CreateChatCompletion method. + CreateChatCompletionFunc func(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) + + // calls tracks calls to the methods. + calls struct { + // CreateChatCompletion holds details about calls to the CreateChatCompletion method. + CreateChatCompletion []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Request is the request argument value. + Request openai.ChatCompletionRequest + } + } + lockCreateChatCompletion sync.RWMutex +} + +// CreateChatCompletion calls CreateChatCompletionFunc. +func (mock *OpenAIClientMock) CreateChatCompletion(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) { + if mock.CreateChatCompletionFunc == nil { + panic("OpenAIClientMock.CreateChatCompletionFunc: method is nil but OpenAIClient.CreateChatCompletion was just called") + } + callInfo := struct { + Ctx context.Context + Request openai.ChatCompletionRequest + }{ + Ctx: ctx, + Request: request, + } + mock.lockCreateChatCompletion.Lock() + mock.calls.CreateChatCompletion = append(mock.calls.CreateChatCompletion, callInfo) + mock.lockCreateChatCompletion.Unlock() + return mock.CreateChatCompletionFunc(ctx, request) +} + +// CreateChatCompletionCalls gets all the calls that were made to CreateChatCompletion. +// Check the length with: +// +// len(mockedOpenAIClient.CreateChatCompletionCalls()) +func (mock *OpenAIClientMock) CreateChatCompletionCalls() []struct { + Ctx context.Context + Request openai.ChatCompletionRequest +} { + var calls []struct { + Ctx context.Context + Request openai.ChatCompletionRequest + } + mock.lockCreateChatCompletion.RLock() + calls = mock.calls.CreateChatCompletion + mock.lockCreateChatCompletion.RUnlock() + return calls +} diff --git a/backend/extractor/readability.go b/backend/extractor/readability.go index 63e1d01..e88e330 100644 --- a/backend/extractor/readability.go +++ b/backend/extractor/readability.go @@ -3,24 +3,34 @@ package extractor import ( "context" + "errors" "fmt" "io" "net/http" "net/url" "regexp" "strings" + "sync" "time" "github.com/PuerkitoBio/goquery" log "github.com/go-pkgz/lgr" "github.com/mauidude/go-readability" + "github.com/sashabaranov/go-openai" "go.mongodb.org/mongo-driver/bson/primitive" "github.com/ukeeper/ukeeper-readability/backend/datastore" ) -// Rules interface with all methods to access datastore -type Rules interface { +//go:generate moq -out openai_mock.go . OpenAIClient + +// OpenAIClient defines interface for OpenAI API client +type OpenAIClient interface { + CreateChatCompletion(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) +} + +// rulesProvider interface with all methods to access datastore +type rulesProvider interface { Get(ctx context.Context, rURL string) (datastore.Rule, bool) GetByID(ctx context.Context, id primitive.ObjectID) (datastore.Rule, bool) Save(ctx context.Context, rule datastore.Rule) (datastore.Rule, error) @@ -28,15 +38,87 @@ type Rules interface { All(ctx context.Context) []datastore.Rule } +// Summaries interface with all methods to access summary cache +// +//go:generate moq -out summaries_mock.go . Summaries +type Summaries interface { + Get(ctx context.Context, content string) (datastore.Summary, bool) + Save(ctx context.Context, summary datastore.Summary) error + Delete(ctx context.Context, contentHash string) error + CleanupExpired(ctx context.Context) (int64, error) +} + +// SummaryMetrics contains metrics related to summary generation +type SummaryMetrics struct { + CacheHits int64 `json:"cache_hits"` + CacheMisses int64 `json:"cache_misses"` + TotalRequests int64 `json:"total_requests"` + FailedRequests int64 `json:"failed_requests"` + AverageResponseMs int64 `json:"average_response_ms"` + TotalResponseTimes time.Duration `json:"-"` // used to calculate average, not exported +} + // UReadability implements fetcher & extractor for local readability-like functionality type UReadability struct { - TimeOut time.Duration - SnippetSize int - Rules Rules + TimeOut time.Duration + SnippetSize int + Rules rulesProvider + Summaries Summaries + OpenAIKey string + ModelType string + OpenAIEnabled bool + SummaryPrompt string + MaxContentLength int + RequestsPerMin int + + apiClient OpenAIClient + rateLimiter *time.Ticker + requestsMutex sync.Mutex + metrics SummaryMetrics + metricsMutex sync.RWMutex +} + +// SetAPIClient sets the API client for testing purposes +func (f *UReadability) SetAPIClient(client OpenAIClient) { + f.apiClient = client +} + +// StartCleanupTask starts a background task to periodically clean up expired summaries +func (f *UReadability) StartCleanupTask(ctx context.Context, interval time.Duration) { + if f.Summaries == nil { + log.Printf("[WARN] summaries store is not configured, cleanup task not started") + return + } + + if interval <= 0 { + interval = 24 * time.Hour // default to daily cleanup + } + + ticker := time.NewTicker(interval) + go func() { + defer ticker.Stop() + for { + select { + case <-ticker.C: + log.Printf("[INFO] running expired summaries cleanup task") + count, err := f.Summaries.CleanupExpired(ctx) + if err != nil { + log.Printf("[ERROR] failed to clean up expired summaries: %v", err) + } else { + log.Printf("[INFO] cleaned up %d expired summaries", count) + } + case <-ctx.Done(): + log.Printf("[INFO] stopping summaries cleanup task") + return + } + } + }() + log.Printf("[INFO] started summaries cleanup task with interval %v", interval) } // Response from api calls type Response struct { + Summary string `json:"summary,omitempty"` Content string `json:"content"` Rich string `json:"rich_content"` Domain string `json:"domain"` @@ -58,6 +140,9 @@ var ( const ( userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15" + + // DefaultSummaryPrompt is the default prompt for generating article summaries + DefaultSummaryPrompt = "You are a helpful assistant that summarizes articles. Please summarize the main points in a few sentences as TLDR style (don't add a TLDR label). Then, list up to five detailed bullet points. Provide the response in plain text. Do not add any additional information. Do not add a Summary at the beginning of the response. If detailed bullet points are too similar to the summary, don't include them at all:" ) // Extract fetches page and retrieves article @@ -70,7 +155,177 @@ func (f *UReadability) ExtractByRule(ctx context.Context, reqURL string, rule *d return f.extractWithRules(ctx, reqURL, rule) } -// ExtractWithRules is the core function that handles extraction with or without a specific rule +// GetMetrics returns the current summary metrics +func (f *UReadability) GetMetrics() SummaryMetrics { + f.metricsMutex.RLock() + defer f.metricsMutex.RUnlock() + + // make a copy to ensure thread safety + metrics := f.metrics + + // calculate average response time if we have any requests + if metrics.TotalRequests > 0 { + metrics.AverageResponseMs = int64(metrics.TotalResponseTimes/time.Millisecond) / metrics.TotalRequests + } + + return metrics +} + +// GenerateSummary creates a summary of the content using OpenAI +func (f *UReadability) GenerateSummary(ctx context.Context, content string) (string, error) { + // check if OpenAI summarization is enabled + if !f.OpenAIEnabled { + return "", errors.New("summary generation is disabled") + } + + // check if API key is available + if f.OpenAIKey == "" { + return "", errors.New("API key for summarization is not set") + } + + // hash content for caching and detecting changes + contentHash := datastore.GenerateContentHash(content) + + // check cache for existing summary + if f.Summaries != nil { + if cachedSummary, found := f.Summaries.Get(ctx, content); found { + // check if summary is valid and not expired + if cachedSummary.ExpiresAt.IsZero() || !time.Now().After(cachedSummary.ExpiresAt) { + log.Printf("[DEBUG] using cached summary for content") + + // track cache hit + f.metricsMutex.Lock() + f.metrics.CacheHits++ + f.metricsMutex.Unlock() + + return cachedSummary.Summary, nil + } + + log.Printf("[DEBUG] cached summary has expired, regenerating") + } + } + + // track cache miss + f.metricsMutex.Lock() + f.metrics.CacheMisses++ + f.metrics.TotalRequests++ + f.metricsMutex.Unlock() + + // apply content length limit if configured + if f.MaxContentLength > 0 && len(content) > f.MaxContentLength { + log.Printf("[DEBUG] content length (%d) exceeds maximum allowed (%d), truncating", len(content), f.MaxContentLength) + content = content[:f.MaxContentLength] + "..." + } + + // initialize API client if not already set + if f.apiClient == nil { + f.apiClient = openai.NewClient(f.OpenAIKey) + } + + // initialize rate limiter if needed and configured + f.requestsMutex.Lock() + shouldThrottle := f.RequestsPerMin > 0 && f.OpenAIKey != "" + if shouldThrottle && f.rateLimiter == nil { + interval := time.Minute / time.Duration(f.RequestsPerMin) + f.rateLimiter = time.NewTicker(interval) + } + f.requestsMutex.Unlock() + + // apply rate limiting if enabled + if shouldThrottle { + select { + case <-f.rateLimiter.C: + // continue with the request + log.Printf("[DEBUG] rate limiter allowed request") + case <-ctx.Done(): + // track failed request due to context cancellation + f.metricsMutex.Lock() + f.metrics.FailedRequests++ + f.metricsMutex.Unlock() + return "", ctx.Err() + } + } + + // set the model to use + model := openai.GPT4oMini + if f.ModelType != "" { + model = f.ModelType + } + + // use custom prompt if provided, otherwise use default + prompt := DefaultSummaryPrompt + if f.SummaryPrompt != "" { + prompt = f.SummaryPrompt + } + + // track response time + startTime := time.Now() + + // make the API request + resp, err := f.apiClient.CreateChatCompletion( + ctx, + openai.ChatCompletionRequest{ + Model: model, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleSystem, + Content: prompt, + }, + { + Role: openai.ChatMessageRoleUser, + Content: content, + }, + }, + }, + ) + + // calculate response time + responseTime := time.Since(startTime) + + if err != nil { + log.Printf("[WARN] AI summarization failed: %v", err) + + // track failed request + f.metricsMutex.Lock() + f.metrics.FailedRequests++ + f.metricsMutex.Unlock() + + return "", fmt.Errorf("failed to generate summary: %w", err) + } + + // update metrics with response time + f.metricsMutex.Lock() + f.metrics.TotalResponseTimes += responseTime + f.metricsMutex.Unlock() + + summary := resp.Choices[0].Message.Content + + // cache the summary if storage is available + if f.Summaries != nil { + // set expiration time to 1 month from now + expiresAt := time.Now().AddDate(0, 1, 0) + + err = f.Summaries.Save(ctx, datastore.Summary{ + ID: contentHash, + Content: content, + Summary: summary, + Model: model, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + ExpiresAt: expiresAt, + }) + + if err != nil { + log.Printf("[WARN] failed to cache summary: %v", err) + } else { + log.Printf("[DEBUG] summary cached successfully") + } + } + + return summary, nil +} + +// extractWithRules is the core function that handles extraction with or without a specific rule func (f *UReadability) extractWithRules(ctx context.Context, reqURL string, rule *datastore.Rule) (*Response, error) { log.Printf("[INFO] extract %s", reqURL) rb := &Response{} @@ -139,6 +394,114 @@ func (f *UReadability) extractWithRules(ctx context.Context, reqURL string, rule return rb, nil } +// ContentParsedWrong handles the logic for when content is parsed incorrectly +func (f *UReadability) ContentParsedWrong(ctx context.Context, urlStr string) (string, error) { + // Extract content using the current method + originalContent, err := f.Extract(ctx, urlStr) + if err != nil { + return "", fmt.Errorf("failed to extract content: %v", err) + } + + // Get CSS selector from ChatGPT + selector, err := f.getChatGPTSelector(ctx, urlStr) + if err != nil { + return "", fmt.Errorf("failed to get CSS selector: %v", err) + } + + // Get the HTML body + body, err := f.getHTMLBody(urlStr) + if err != nil { + return "", fmt.Errorf("failed to get HTML body: %v", err) + } + + // Extract content using the new selector + newContent, err := f.extractContentWithSelector(body, selector) + if err != nil { + return "", fmt.Errorf("failed to extract content with new selector: %v", err) + } + + // Compare original and new content + if strings.TrimSpace(originalContent.Content) != strings.TrimSpace(newContent) { + // Contents are different, create a new rule + rule := datastore.Rule{ + Author: "", + Domain: f.extractDomain(urlStr), + Content: selector, + TestURLs: []string{urlStr}, + Enabled: true, + } + + _, err = f.Rules.Save(ctx, rule) + if err != nil { + return "", fmt.Errorf("failed to save new rule: %v", err) + } + + return fmt.Sprintf("new custom rule with DOM %s created", selector), nil + } + + return "default rule is good, no need to create the custom one", nil +} + +func (f *UReadability) getChatGPTSelector(ctx context.Context, urlStr string) (string, error) { + client := openai.NewClient(f.OpenAIKey) + resp, err := client.CreateChatCompletion( + ctx, + openai.ChatCompletionRequest{ + Model: openai.GPT4o, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleSystem, + Content: "You are a helpful assistant that provides CSS selectors for extracting main content from web pages.", + }, + { + Role: openai.ChatMessageRoleUser, + Content: fmt.Sprintf("Given the URL %s, identify the CSS selector that can be used to extract the main content of the article. This typically includes elements like 'article', 'main', or specific classes. Return only this selector and nothing else.", urlStr), + }, + }, + }, + ) + + if err != nil { + return "", err + } + + return resp.Choices[0].Message.Content, nil +} + +func (f *UReadability) getHTMLBody(urlStr string) (string, error) { + //nolint:gosec + resp, err := http.Get(urlStr) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return string(body), nil +} + +func (f *UReadability) extractContentWithSelector(body, selector string) (string, error) { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)) + if err != nil { + return "", err + } + + content := doc.Find(selector).Text() + return content, nil +} + +func (f *UReadability) extractDomain(urlStr string) string { + u, err := url.Parse(urlStr) + if err != nil { + return "" + } + return u.Hostname() +} + // getContent retrieves content from raw body string, both content (text only) and rich (with html tags) // if rule is provided, it uses custom rule, otherwise tries to retrieve one from the storage, // and at last tries to use general readability parser diff --git a/backend/extractor/readability_test.go b/backend/extractor/readability_test.go index 1c2ff3e..e6a0bbe 100644 --- a/backend/extractor/readability_test.go +++ b/backend/extractor/readability_test.go @@ -2,6 +2,7 @@ package extractor import ( "context" + "fmt" "io" "log" "net/http" @@ -11,6 +12,7 @@ import ( "testing" "time" + "github.com/sashabaranov/go-openai" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson/primitive" @@ -207,3 +209,157 @@ func TestGetContentCustom(t *testing.T) { assert.Len(t, content, 6988) assert.Len(t, rich, 7169) } + +func TestUReadability_GenerateSummary(t *testing.T) { + mockOpenAI := &OpenAIClientMock{ + CreateChatCompletionFunc: func(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) { + return openai.ChatCompletionResponse{ + Choices: []openai.ChatCompletionChoice{ + { + Message: openai.ChatCompletionMessage{ + Content: "This is a summary of the article.", + }, + }, + }, + }, nil + }, + } + + // cache hit test setup + cachedContent := "This is a cached content" + mockSummariesWithCache := &SummariesMock{ + GetFunc: func(ctx context.Context, content string) (datastore.Summary, bool) { + if content == cachedContent { + return datastore.Summary{ + ID: "cached-id", + Content: cachedContent, + Summary: "This is a cached summary.", + Model: "mini", + CreatedAt: time.Now(), + }, true + } + return datastore.Summary{}, false + }, + } + + // cache miss and save test setup + var savedSummary datastore.Summary + mockSummariesWithSave := &SummariesMock{ + GetFunc: func(ctx context.Context, content string) (datastore.Summary, bool) { + return datastore.Summary{}, false + }, + SaveFunc: func(ctx context.Context, summary datastore.Summary) error { + savedSummary = summary + return nil + }, + } + + // cache save error test setup + mockSummariesWithError := &SummariesMock{ + GetFunc: func(ctx context.Context, content string) (datastore.Summary, bool) { + return datastore.Summary{}, false + }, + SaveFunc: func(ctx context.Context, summary datastore.Summary) error { + return fmt.Errorf("failed to save summary") + }, + } + + tests := []struct { + name string + content string + apiKey string + modelType string + summaries Summaries + expectedResult string + expectedError string + checkSaved bool + }{ + { + name: "Valid API Key and content", + content: "This is a test article content.", + apiKey: "test-key", + expectedResult: "This is a summary of the article.", + expectedError: "", + }, + { + name: "No API Key", + content: "This is a test article content.", + apiKey: "", + expectedResult: "", + expectedError: "API key for summarization is not set", + }, + { + name: "Cache hit", + content: cachedContent, + apiKey: "test-key", + summaries: mockSummariesWithCache, + expectedResult: "This is a cached summary.", + expectedError: "", + }, + { + name: "Cache miss with save", + content: "This is uncached content", + apiKey: "test-key", + modelType: "non-default", + summaries: mockSummariesWithSave, + expectedResult: "This is a summary of the article.", + expectedError: "", + checkSaved: true, + }, + { + name: "Cache save error", + content: "This is uncached content", + apiKey: "test-key", + summaries: mockSummariesWithError, + expectedResult: "This is a summary of the article.", + expectedError: "", + }, + { + name: "Direct model specification", + content: "This is a test with direct model name.", + apiKey: "test-key", + modelType: "gpt-4o", // direct model specification + expectedResult: "This is a summary of the article.", + expectedError: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + readability := UReadability{ + OpenAIKey: tt.apiKey, + ModelType: tt.modelType, + Summaries: tt.summaries, + apiClient: mockOpenAI, + OpenAIEnabled: true, // enable OpenAI for tests + } + + result, err := readability.GenerateSummary(context.Background(), tt.content) + + if tt.expectedError != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedResult, result) + } + + if tt.checkSaved { + truncateLength := 1000 + expectedContent := tt.content + if len(tt.content) > truncateLength { + expectedContent = tt.content[:truncateLength] + } + assert.Equal(t, expectedContent, savedSummary.Content) + assert.Equal(t, "This is a summary of the article.", savedSummary.Summary) + + // check the saved model info + expectedModel := tt.modelType + if tt.modelType == "" { + expectedModel = openai.GPT4oMini // default model + } + assert.Equal(t, expectedModel, savedSummary.Model) + } + }) + } +} diff --git a/backend/extractor/summaries_mock.go b/backend/extractor/summaries_mock.go new file mode 100644 index 0000000..ac90dcd --- /dev/null +++ b/backend/extractor/summaries_mock.go @@ -0,0 +1,226 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package extractor + +import ( + "context" + "github.com/ukeeper/ukeeper-readability/backend/datastore" + "sync" +) + +// Ensure, that SummariesMock does implement Summaries. +// If this is not the case, regenerate this file with moq. +var _ Summaries = &SummariesMock{} + +// SummariesMock is a mock implementation of Summaries. +// +// func TestSomethingThatUsesSummaries(t *testing.T) { +// +// // make and configure a mocked Summaries +// mockedSummaries := &SummariesMock{ +// CleanupExpiredFunc: func(ctx context.Context) (int64, error) { +// panic("mock out the CleanupExpired method") +// }, +// DeleteFunc: func(ctx context.Context, contentHash string) error { +// panic("mock out the Delete method") +// }, +// GetFunc: func(ctx context.Context, content string) (datastore.Summary, bool) { +// panic("mock out the Get method") +// }, +// SaveFunc: func(ctx context.Context, summary datastore.Summary) error { +// panic("mock out the Save method") +// }, +// } +// +// // use mockedSummaries in code that requires Summaries +// // and then make assertions. +// +// } +type SummariesMock struct { + // CleanupExpiredFunc mocks the CleanupExpired method. + CleanupExpiredFunc func(ctx context.Context) (int64, error) + + // DeleteFunc mocks the Delete method. + DeleteFunc func(ctx context.Context, contentHash string) error + + // GetFunc mocks the Get method. + GetFunc func(ctx context.Context, content string) (datastore.Summary, bool) + + // SaveFunc mocks the Save method. + SaveFunc func(ctx context.Context, summary datastore.Summary) error + + // calls tracks calls to the methods. + calls struct { + // CleanupExpired holds details about calls to the CleanupExpired method. + CleanupExpired []struct { + // Ctx is the ctx argument value. + Ctx context.Context + } + // Delete holds details about calls to the Delete method. + Delete []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // ContentHash is the contentHash argument value. + ContentHash string + } + // Get holds details about calls to the Get method. + Get []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Content is the content argument value. + Content string + } + // Save holds details about calls to the Save method. + Save []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Summary is the summary argument value. + Summary datastore.Summary + } + } + lockCleanupExpired sync.RWMutex + lockDelete sync.RWMutex + lockGet sync.RWMutex + lockSave sync.RWMutex +} + +// CleanupExpired calls CleanupExpiredFunc. +func (mock *SummariesMock) CleanupExpired(ctx context.Context) (int64, error) { + if mock.CleanupExpiredFunc == nil { + panic("SummariesMock.CleanupExpiredFunc: method is nil but Summaries.CleanupExpired was just called") + } + callInfo := struct { + Ctx context.Context + }{ + Ctx: ctx, + } + mock.lockCleanupExpired.Lock() + mock.calls.CleanupExpired = append(mock.calls.CleanupExpired, callInfo) + mock.lockCleanupExpired.Unlock() + return mock.CleanupExpiredFunc(ctx) +} + +// CleanupExpiredCalls gets all the calls that were made to CleanupExpired. +// Check the length with: +// +// len(mockedSummaries.CleanupExpiredCalls()) +func (mock *SummariesMock) CleanupExpiredCalls() []struct { + Ctx context.Context +} { + var calls []struct { + Ctx context.Context + } + mock.lockCleanupExpired.RLock() + calls = mock.calls.CleanupExpired + mock.lockCleanupExpired.RUnlock() + return calls +} + +// Delete calls DeleteFunc. +func (mock *SummariesMock) Delete(ctx context.Context, contentHash string) error { + if mock.DeleteFunc == nil { + panic("SummariesMock.DeleteFunc: method is nil but Summaries.Delete was just called") + } + callInfo := struct { + Ctx context.Context + ContentHash string + }{ + Ctx: ctx, + ContentHash: contentHash, + } + mock.lockDelete.Lock() + mock.calls.Delete = append(mock.calls.Delete, callInfo) + mock.lockDelete.Unlock() + return mock.DeleteFunc(ctx, contentHash) +} + +// DeleteCalls gets all the calls that were made to Delete. +// Check the length with: +// +// len(mockedSummaries.DeleteCalls()) +func (mock *SummariesMock) DeleteCalls() []struct { + Ctx context.Context + ContentHash string +} { + var calls []struct { + Ctx context.Context + ContentHash string + } + mock.lockDelete.RLock() + calls = mock.calls.Delete + mock.lockDelete.RUnlock() + return calls +} + +// Get calls GetFunc. +func (mock *SummariesMock) Get(ctx context.Context, content string) (datastore.Summary, bool) { + if mock.GetFunc == nil { + panic("SummariesMock.GetFunc: method is nil but Summaries.Get was just called") + } + callInfo := struct { + Ctx context.Context + Content string + }{ + Ctx: ctx, + Content: content, + } + mock.lockGet.Lock() + mock.calls.Get = append(mock.calls.Get, callInfo) + mock.lockGet.Unlock() + return mock.GetFunc(ctx, content) +} + +// GetCalls gets all the calls that were made to Get. +// Check the length with: +// +// len(mockedSummaries.GetCalls()) +func (mock *SummariesMock) GetCalls() []struct { + Ctx context.Context + Content string +} { + var calls []struct { + Ctx context.Context + Content string + } + mock.lockGet.RLock() + calls = mock.calls.Get + mock.lockGet.RUnlock() + return calls +} + +// Save calls SaveFunc. +func (mock *SummariesMock) Save(ctx context.Context, summary datastore.Summary) error { + if mock.SaveFunc == nil { + panic("SummariesMock.SaveFunc: method is nil but Summaries.Save was just called") + } + callInfo := struct { + Ctx context.Context + Summary datastore.Summary + }{ + Ctx: ctx, + Summary: summary, + } + mock.lockSave.Lock() + mock.calls.Save = append(mock.calls.Save, callInfo) + mock.lockSave.Unlock() + return mock.SaveFunc(ctx, summary) +} + +// SaveCalls gets all the calls that were made to Save. +// Check the length with: +// +// len(mockedSummaries.SaveCalls()) +func (mock *SummariesMock) SaveCalls() []struct { + Ctx context.Context + Summary datastore.Summary +} { + var calls []struct { + Ctx context.Context + Summary datastore.Summary + } + mock.lockSave.RLock() + calls = mock.calls.Save + mock.lockSave.RUnlock() + return calls +} diff --git a/backend/go.mod b/backend/go.mod index dd0dd6b..74fd979 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,6 +1,6 @@ module github.com/ukeeper/ukeeper-readability/backend -go 1.23.0 +go 1.24.0 toolchain go1.24.2 @@ -12,6 +12,7 @@ require ( github.com/jessevdk/go-flags v1.6.1 github.com/kennygrant/sanitize v1.2.4 github.com/mauidude/go-readability v0.0.0-20220221173116-a9b3620098b7 + github.com/sashabaranov/go-openai v1.38.2 github.com/stretchr/testify v1.10.0 go.mongodb.org/mongo-driver v1.17.3 golang.org/x/net v0.38.0 diff --git a/backend/go.sum b/backend/go.sum index 6c0a23b..c7da241 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -168,6 +168,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sashabaranov/go-openai v1.38.2 h1:akrssjj+6DY3lWuDwHv6cBvJ8Z+FZDM9XEaaYFt0Auo= +github.com/sashabaranov/go-openai v1.38.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= diff --git a/backend/main.go b/backend/main.go index 7fe1898..633fe2b 100644 --- a/backend/main.go +++ b/backend/main.go @@ -18,20 +18,34 @@ import ( var revision string +// OpenAIGroup contains settings for OpenAI integration +type OpenAIGroup struct { + DisableSummaries bool `long:"disable-summaries" env:"DISABLE_SUMMARIES" description:"disable summary generation with OpenAI"` + APIKey string `long:"api-key" env:"API_KEY" description:"OpenAI API key for summary generation"` + ModelType string `long:"model-type" env:"MODEL_TYPE" default:"gpt-4o-mini" description:"OpenAI model name for summary generation (e.g., gpt-4o, gpt-4o-mini)"` + SummaryPrompt string `long:"summary-prompt" env:"SUMMARY_PROMPT" description:"custom prompt for summary generation (default is used if not specified)"` + MaxContentLength int `long:"max-content-length" env:"MAX_CONTENT_LENGTH" default:"10000" description:"maximum content length to send to OpenAI API (0 for no limit)"` + RequestsPerMinute int `long:"requests-per-minute" env:"REQUESTS_PER_MINUTE" default:"10" description:"maximum number of OpenAI API requests per minute (0 for no limit)"` + CleanupInterval time.Duration `long:"cleanup-interval" env:"CLEANUP_INTERVAL" default:"24h" description:"interval for cleaning up expired summaries"` +} + var opts struct { Address string `long:"address" env:"UKEEPER_ADDRESS" default:"" description:"listening address"` Port int `long:"port" env:"UKEEPER_PORT" default:"8080" description:"port"` FrontendDir string `long:"frontend-dir" env:"FRONTEND_DIR" default:"/srv/web" description:"directory with frontend templates and static/ directory for static assets"` Credentials map[string]string `long:"creds" env:"CREDS" description:"credentials for protected calls (POST, DELETE /rules)"` Token string `long:"token" env:"UKEEPER_TOKEN" description:"token for /content/v1/parser endpoint auth"` - MongoURI string `short:"m" long:"mongo-uri" env:"MONGO_URI" required:"true" description:"MongoDB connection string"` + MongoURI string `long:"mongo-uri" env:"MONGO_URI" required:"true" description:"MongoDB connection string"` MongoDelay time.Duration `long:"mongo-delay" env:"MONGO_DELAY" default:"0" description:"mongo initial delay"` MongoDB string `long:"mongo-db" env:"MONGO_DB" default:"ureadability" description:"mongo database name"` Debug bool `long:"dbg" env:"DEBUG" description:"debug mode"` + + OpenAI OpenAIGroup `group:"openai" namespace:"openai" env-namespace:"OPENAI" description:"OpenAI integration settings"` } func main() { if _, err := flags.Parse(&opts); err != nil { + log.Printf("[ERROR] can't parse command line flags, %v", err) os.Exit(1) } var options []log.Option @@ -49,9 +63,16 @@ func main() { stores := db.GetStores() srv := rest.Server{ Readability: extractor.UReadability{ - TimeOut: 30 * time.Second, - SnippetSize: 300, - Rules: stores.Rules, + TimeOut: 30 * time.Second, + SnippetSize: 300, + Rules: stores.Rules, + Summaries: stores.Summaries, + OpenAIKey: opts.OpenAI.APIKey, + ModelType: opts.OpenAI.ModelType, + OpenAIEnabled: !opts.OpenAI.DisableSummaries, + SummaryPrompt: opts.OpenAI.SummaryPrompt, + MaxContentLength: opts.OpenAI.MaxContentLength, + RequestsPerMin: opts.OpenAI.RequestsPerMinute, }, Token: opts.Token, Credentials: opts.Credentials, @@ -67,5 +88,10 @@ func main() { cancel() }() + // start summary cleanup task if OpenAI is enabled + if !opts.OpenAI.DisableSummaries { + srv.Readability.StartCleanupTask(ctx, opts.OpenAI.CleanupInterval) + } + srv.Run(ctx, opts.Address, opts.Port, opts.FrontendDir) } diff --git a/backend/rest/server.go b/backend/rest/server.go index 153de0e..f065135 100644 --- a/backend/rest/server.go +++ b/backend/rest/server.go @@ -6,9 +6,11 @@ import ( "crypto/subtle" "fmt" "html/template" + "math" "net/http" "os" "path/filepath" + "strconv" "strings" "time" @@ -77,6 +79,8 @@ func (s *Server) routes(frontendDir string) http.Handler { api.HandleFunc("GET /content/v1/parser", s.extractArticleEmulateReadability) api.HandleFunc("POST /extract", s.extractArticle) api.HandleFunc("POST /auth", s.authFake) + api.HandleFunc("GET /metrics", s.handleMetrics) + api.HandleFunc("GET /content-parsed-wrong", s.contentParsedWrong) // add protected group with its own set of middlewares protectedGroup := api.Group() @@ -176,12 +180,25 @@ func (s *Server) extractArticle(w http.ResponseWriter, r *http.Request) { // if token is not set for application, it won't be checked func (s *Server) extractArticleEmulateReadability(w http.ResponseWriter, r *http.Request) { token := r.URL.Query().Get("token") + summary, _ := strconv.ParseBool(r.URL.Query().Get("summary")) if s.Token != "" && token == "" { rest.SendErrorJSON(w, r, log.Default(), http.StatusExpectationFailed, nil, "no token passed") return } + // check if summary is requested but token is not provided, or API key is not set + if summary { + if s.Readability.OpenAIKey == "" { + rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, nil, "OpenAI key is not set") + return + } + if s.Token == "" { + rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, nil, "summary generation requires token, but token is not set for the server") + return + } + } + if s.Token != "" && s.Token != token { rest.SendErrorJSON(w, r, log.Default(), http.StatusUnauthorized, nil, "wrong token passed") return @@ -199,6 +216,24 @@ func (s *Server) extractArticleEmulateReadability(w http.ResponseWriter, r *http return } + if summary { + summaryText, err := s.Readability.GenerateSummary(r.Context(), res.Content) + if err != nil { + rest.SendErrorJSON(w, r, log.Default(), http.StatusInternalServerError, err, fmt.Sprintf("failed to generate summary: %v", err)) + return + } + res.Summary = summaryText + } + + if summary { + summaryText, err := s.Readability.GenerateSummary(r.Context(), res.Content) + if err != nil { + rest.SendErrorJSON(w, r, log.Default(), http.StatusInternalServerError, err, fmt.Sprintf("failed to generate summary: %v", err)) + return + } + res.Summary = summaryText + } + rest.RenderJSON(w, &res) } @@ -238,6 +273,16 @@ func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) { continue } + // generate summary if API key is available + if s.Readability.OpenAIKey != "" { + result.Summary, e = s.Readability.GenerateSummary(r.Context(), result.Content) + if e != nil { + log.Printf("[WARN] failed to generate summary for preview of %s: %v", url, e) + } else { + log.Printf("[DEBUG] summary generated successfully for preview of %s", url) + } + } + responses = append(responses, *result) } @@ -248,6 +293,7 @@ func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) { Excerpt string Rich template.HTML Content string + Summary template.HTML } results := make([]result, 0, len(responses)) @@ -259,6 +305,8 @@ func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) { //nolint:gosec // this content is escaped by Extractor, so it's safe to use it as is Rich: template.HTML(r.Rich), Content: r.Content, + //nolint:gosec // we do not expect CSS from OpenAI response + Summary: template.HTML(strings.ReplaceAll(r.Summary, "\n", "
")), }) } @@ -345,6 +393,53 @@ func (s *Server) authFake(w http.ResponseWriter, _ *http.Request) { rest.RenderJSON(w, JSON{"pong": t.Format("20060102150405")}) } +// handleMetrics returns summary generation metrics +func (s *Server) handleMetrics(w http.ResponseWriter, _ *http.Request) { + metrics := s.Readability.GetMetrics() + + // calculate hit ratio + hitRatio := float64(0) + if metrics.CacheHits+metrics.CacheMisses > 0 { + hitRatio = float64(metrics.CacheHits) / float64(metrics.CacheHits+metrics.CacheMisses) + } + + // get metrics from the UReadability instance + rest.RenderJSON(w, JSON{ + "summary": JSON{ + "cache_hits": metrics.CacheHits, + "cache_misses": metrics.CacheMisses, + "cache_hit_ratio": hitRatio, + "total_requests": metrics.TotalRequests, + "failed_requests": metrics.FailedRequests, + "average_response_ms": metrics.AverageResponseMs, + "success_rate": float64(metrics.TotalRequests-metrics.FailedRequests) / float64(math.Max(1, float64(metrics.TotalRequests))), + }, + "version": s.Version, + "time": time.Now().Format(time.RFC3339), + }) +} + +func (s *Server) contentParsedWrong(w http.ResponseWriter, r *http.Request) { + if s.Readability.OpenAIKey == "" { + rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, nil, "OpenAI key is not set") + return + } + + exampleURL := r.URL.Query().Get("url") + if exampleURL == "" { + rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, nil, "url parameter is required") + return + } + + message, err := s.Readability.ContentParsedWrong(r.Context(), exampleURL) + if err != nil { + rest.SendErrorJSON(w, r, log.Default(), http.StatusInternalServerError, err, err.Error()) + return + } + + rest.RenderJSON(w, JSON{"message": message}) +} + func getBid(id string) primitive.ObjectID { bid, err := primitive.ObjectIDFromHex(id) if err != nil { diff --git a/backend/rest/server_test.go b/backend/rest/server_test.go index 35b88d1..7cde035 100644 --- a/backend/rest/server_test.go +++ b/backend/rest/server_test.go @@ -16,6 +16,7 @@ import ( "time" "github.com/go-pkgz/rest" + "github.com/sashabaranov/go-openai" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -581,6 +582,172 @@ func TestServer_Preview(t *testing.T) { assert.Contains(t, string(b), "Failed to parse form") } +func TestServer_ExtractArticleEmulateReadabilityWithSummaryFailures(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("

This is a test article.

")) + })) + defer ts.Close() + + // create a mock version of the Summaries interface + mockSummaries := &extractor.SummariesMock{ + GetFunc: func(ctx context.Context, content string) (datastore.Summary, bool) { + return datastore.Summary{}, false + }, + SaveFunc: func(ctx context.Context, summary datastore.Summary) error { + return nil + }, + } + + tests := []struct { + name string + serverToken string + url string + token string + summary bool + expectedStatus int + expectedError string + openAIKey string + openAIModel string + }{ + { + name: "Valid token and summary, no OpenAI key", + serverToken: "secret", + url: ts.URL, + token: "secret", + summary: true, + expectedStatus: http.StatusBadRequest, + expectedError: "OpenAI key is not set", + }, + { + name: "No token, summary requested", + serverToken: "secret", + url: ts.URL, + summary: true, + expectedStatus: http.StatusExpectationFailed, + expectedError: "no token passed", + }, + { + name: "Invalid token, summary requested", + serverToken: "secret", + url: ts.URL, + token: "wrong", + summary: true, + expectedStatus: http.StatusUnauthorized, + expectedError: "wrong token passed", + openAIKey: "test key", + }, + { + name: "Valid token, no summary", + serverToken: "secret", + url: ts.URL, + token: "secret", + expectedStatus: http.StatusOK, + }, + { + name: "No token, no summary", + serverToken: "secret", + url: ts.URL, + expectedStatus: http.StatusExpectationFailed, + }, + { + name: "Server token not set, summary requested", + serverToken: "", + url: ts.URL, + token: "any", + summary: true, + expectedStatus: http.StatusBadRequest, + expectedError: "summary generation requires token, but token is not set for the server", + openAIKey: "test key", + }, + { + name: "Server token not set, no summary", + serverToken: "", + url: ts.URL, + expectedStatus: http.StatusOK, + }, + { + name: "Valid token and summary with custom model", + serverToken: "secret", + url: ts.URL, + token: "secret", + summary: true, + expectedStatus: http.StatusOK, + openAIKey: "test key", + openAIModel: "gpt-4o", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := Server{ + Readability: extractor.UReadability{ + TimeOut: 30 * time.Second, + SnippetSize: 300, + Rules: nil, + Summaries: mockSummaries, + OpenAIKey: tt.openAIKey, + OpenAIEnabled: true, // enable OpenAI for tests + ModelType: tt.openAIModel, + }, + Token: tt.serverToken, + } + + // set the API client for testing + mockClient := &extractor.OpenAIClientMock{ + CreateChatCompletionFunc: func(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) { + model := "gpt-4o-mini" + if tt.openAIModel != "" { + model = tt.openAIModel + } + assert.Equal(t, model, request.Model) + return openai.ChatCompletionResponse{ + Choices: []openai.ChatCompletionChoice{ + { + Message: openai.ChatCompletionMessage{ + Content: "This is a summary of the article.", + }, + }, + }, + }, nil + }, + } + srv.Readability.SetAPIClient(mockClient) + + //} + + url := fmt.Sprintf("/api/content/v1/parser?url=%s", tt.url) + if tt.token != "" { + url += fmt.Sprintf("&token=%s", tt.token) + } + if tt.summary { + url += "&summary=true" + } + + req, err := http.NewRequest("GET", url, nil) + require.NoError(t, err) + + rr := httptest.NewRecorder() + srv.extractArticleEmulateReadability(rr, req) + + assert.Equal(t, tt.expectedStatus, rr.Code, rr.Body.String()) + + if tt.expectedError != "" { + var errorResponse map[string]string + err = json.Unmarshal(rr.Body.Bytes(), &errorResponse) + require.NoError(t, err) + assert.Equal(t, tt.expectedError, errorResponse["error"]) + } else if tt.summary && tt.openAIKey != "" { + var response extractor.Response + err = json.Unmarshal(rr.Body.Bytes(), &response) + require.NoError(t, err) + assert.NotEmpty(t, response.Content) + assert.Equal(t, "This is a summary of the article.", response.Summary) + } + }) + } +} + func get(t *testing.T, url string) (response string, statusCode int) { r, err := http.Get(url) require.NoError(t, err) @@ -622,6 +789,8 @@ func startupT(t *testing.T) (*httptest.Server, *Server) { TimeOut: 30 * time.Second, SnippetSize: 300, Rules: stores.Rules, + Summaries: stores.Summaries, + ModelType: "mini", }, Credentials: map[string]string{"admin": "password"}, Version: "dev-test", diff --git a/backend/vendor/github.com/sashabaranov/go-openai/.gitignore b/backend/vendor/github.com/sashabaranov/go-openai/.gitignore new file mode 100644 index 0000000..b0ac160 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/.gitignore @@ -0,0 +1,22 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Auth token for tests +.openai-token +.idea + +# Generated by tests +test.mp3 \ No newline at end of file diff --git a/backend/vendor/github.com/sashabaranov/go-openai/.golangci.yml b/backend/vendor/github.com/sashabaranov/go-openai/.golangci.yml new file mode 100644 index 0000000..9f2ba52 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/.golangci.yml @@ -0,0 +1,259 @@ +## Golden config for golangci-lint v1.47.3 +# +# This is the best config for golangci-lint based on my experience and opinion. +# It is very strict, but not extremely strict. +# Feel free to adopt and change it for your needs. + +run: + # Timeout for analysis, e.g. 30s, 5m. + # Default: 1m + timeout: 3m + + +# This file contains only configs which differ from defaults. +# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml +linters-settings: + cyclop: + # The maximal code complexity to report. + # Default: 10 + max-complexity: 30 + # The maximal average package complexity. + # If it's higher than 0.0 (float) the check is enabled + # Default: 0.0 + package-average: 10.0 + + errcheck: + # Report about not checking of errors in type assertions: `a := b.(MyStruct)`. + # Such cases aren't reported by default. + # Default: false + check-type-assertions: true + + funlen: + # Checks the number of lines in a function. + # If lower than 0, disable the check. + # Default: 60 + lines: 100 + # Checks the number of statements in a function. + # If lower than 0, disable the check. + # Default: 40 + statements: 50 + + gocognit: + # Minimal code complexity to report + # Default: 30 (but we recommend 10-20) + min-complexity: 20 + + gocritic: + # Settings passed to gocritic. + # The settings key is the name of a supported gocritic checker. + # The list of supported checkers can be find in https://go-critic.github.io/overview. + settings: + captLocal: + # Whether to restrict checker to params only. + # Default: true + paramsOnly: false + underef: + # Whether to skip (*x).method() calls where x is a pointer receiver. + # Default: true + skipRecvDeref: false + + mnd: + # List of function patterns to exclude from analysis. + # Values always ignored: `time.Date` + # Default: [] + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets + - prometheus.ExponentialBucketsRange + - prometheus.LinearBuckets + - strconv.FormatFloat + - strconv.FormatInt + - strconv.FormatUint + - strconv.ParseFloat + - strconv.ParseInt + - strconv.ParseUint + + gomodguard: + blocked: + # List of blocked modules. + # Default: [] + modules: + - github.com/golang/protobuf: + recommendations: + - google.golang.org/protobuf + reason: "see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules" + - github.com/satori/go.uuid: + recommendations: + - github.com/google/uuid + reason: "satori's package is not maintained" + - github.com/gofrs/uuid: + recommendations: + - github.com/google/uuid + reason: "see recommendation from dev-infra team: https://confluence.gtforge.com/x/gQI6Aw" + + govet: + # Enable all analyzers. + # Default: false + enable-all: true + # Disable analyzers by name. + # Run `go tool vet help` to see all analyzers. + # Default: [] + disable: + - fieldalignment # too strict + # Settings per analyzer. + settings: + shadow: + # Whether to be strict about shadowing; can be noisy. + # Default: false + strict: true + + nakedret: + # Make an issue if func has more lines of code than this setting, and it has naked returns. + # Default: 30 + max-func-lines: 0 + + nolintlint: + # Exclude following linters from requiring an explanation. + # Default: [] + allow-no-explanation: [ funlen, gocognit, lll ] + # Enable to require an explanation of nonzero length after each nolint directive. + # Default: false + require-explanation: true + # Enable to require nolint directives to mention the specific linter being suppressed. + # Default: false + require-specific: true + + rowserrcheck: + # database/sql is always checked + # Default: [] + packages: + - github.com/jmoiron/sqlx + + tenv: + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. + # Default: false + all: true + + +linters: + disable-all: true + enable: + ## enabled by default + - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases + - gosimple # Linter for Go source code that specializes in simplifying a code + - govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string + - ineffassign # Detects when assignments to existing variables are not used + - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks + - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code + - unused # Checks Go code for unused constants, variables, functions and types + ## disabled by default + # - asasalint # Check for pass []any as any in variadic func(...any) + - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers + - bidichk # Checks for dangerous unicode character sequences + - bodyclose # checks whether HTTP response body is closed successfully + - contextcheck # check the function whether use a non-inherited context + - cyclop # checks function and package cyclomatic complexity + - dupl # Tool for code clone detection + - durationcheck # check for two durations multiplied together + - errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error. + - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. + - exhaustive # check exhaustiveness of enum switch statements + - forbidigo # Forbids identifiers + - funlen # Tool for detection of long functions + # - gochecknoglobals # check that no global variables exist + - gochecknoinits # Checks that no init functions are present in Go code + - gocognit # Computes and checks the cognitive complexity of functions + - goconst # Finds repeated strings that could be replaced by a constant + - gocritic # Provides diagnostics that check for bugs, performance and style issues. + - gocyclo # Computes and checks the cyclomatic complexity of functions + - godot # Check if comments end in a period + - goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt. + - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. + - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. + - goprintffuncname # Checks that printf-like functions are named with f at the end + - gosec # Inspects source code for security problems + - lll # Reports long lines + - makezero # Finds slice declarations with non-zero initial length + # - nakedret # Finds naked returns in functions greater than a specified function length + - mnd # An analyzer to detect magic numbers. + - nestif # Reports deeply nested if statements + - nilerr # Finds the code that returns nil even if it checks that the error is not nil. + - nilnil # Checks that there is no simultaneous return of nil error and an invalid value. + # - noctx # noctx finds sending http request without context.Context + - nolintlint # Reports ill-formed or insufficient nolint directives + # - nonamedreturns # Reports all named returns + - nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. + - predeclared # find code that shadows one of Go's predeclared identifiers + - promlinter # Check Prometheus metrics naming via promlint + - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. + - rowserrcheck # checks whether Err of rows is checked successfully + - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. + - stylecheck # Stylecheck is a replacement for golint + - testpackage # linter that makes you use a separate _test package + - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes + - unconvert # Remove unnecessary type conversions + - unparam # Reports unused function parameters + - usetesting # Reports uses of functions with replacement inside the testing package + - wastedassign # wastedassign finds wasted assignment statements. + - whitespace # Tool for detection of leading and trailing whitespace + ## you may want to enable + #- decorder # check declaration order and count of types, constants, variables and functions + #- exhaustruct # Checks if all structure fields are initialized + #- goheader # Checks is file header matches to pattern + #- ireturn # Accept Interfaces, Return Concrete Types + #- prealloc # [premature optimization, but can be used in some cases] Finds slice declarations that could potentially be preallocated + #- varnamelen # [great idea, but too many false positives] checks that the length of a variable's name matches its scope + #- wrapcheck # Checks that errors returned from external packages are wrapped + ## disabled + #- containedctx # containedctx is a linter that detects struct contained context.Context field + #- depguard # [replaced by gomodguard] Go linter that checks if package imports are in a list of acceptable packages + #- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) + #- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. + #- forcetypeassert # [replaced by errcheck] finds forced type assertions + #- gci # Gci controls golang package import order and makes it always deterministic. + #- godox # Tool for detection of FIXME, TODO and other comment keywords + #- goerr113 # [too strict] Golang linter to check the errors handling expressions + #- gofmt # [replaced by goimports] Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification + #- gofumpt # [replaced by goimports, gofumports is not available yet] Gofumpt checks whether code was gofumpt-ed. + #- grouper # An analyzer to analyze expression groups. + #- ifshort # Checks that your code uses short syntax for if-statements whenever possible + #- importas # Enforces consistent import aliases + #- maintidx # maintidx measures the maintainability index of each function. + #- misspell # [useless] Finds commonly misspelled English words in comments + #- nlreturn # [too strict and mostly code is not more readable] nlreturn checks for a new line before return and branch statements to increase code clarity + #- nosnakecase # Detects snake case of variable naming and function name. # TODO: maybe enable after https://github.com/sivchari/nosnakecase/issues/14 + #- paralleltest # [too many false positives] paralleltest detects missing usage of t.Parallel() method in your Go test + #- tagliatelle # Checks the struct tags. + #- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers + #- wsl # [too strict and mostly code is not more readable] Whitespace Linter - Forces you to use empty lines! + + +issues: + # Maximum count of issues with the same text. + # Set to 0 to disable. + # Default: 3 + max-same-issues: 50 + + exclude-rules: + - source: "^//\\s*go:generate\\s" + linters: [ lll ] + - source: "(noinspection|TODO)" + linters: [ godot ] + - source: "//noinspection" + linters: [ gocritic ] + - source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {" + linters: [ errorlint ] + - path: "_test\\.go" + linters: + - bodyclose + - dupl + - funlen + - goconst + - gosec + - noctx + - wrapcheck diff --git a/backend/vendor/github.com/sashabaranov/go-openai/CONTRIBUTING.md b/backend/vendor/github.com/sashabaranov/go-openai/CONTRIBUTING.md new file mode 100644 index 0000000..4dd1840 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/CONTRIBUTING.md @@ -0,0 +1,88 @@ +# Contributing Guidelines + +## Overview +Thank you for your interest in contributing to the "Go OpenAI" project! By following this guideline, we hope to ensure that your contributions are made smoothly and efficiently. The Go OpenAI project is licensed under the [Apache 2.0 License](https://github.com/sashabaranov/go-openai/blob/master/LICENSE), and we welcome contributions through GitHub pull requests. + +## Reporting Bugs +If you discover a bug, first check the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to see if the issue has already been reported. If you're reporting a new issue, please use the "Bug report" template and provide detailed information about the problem, including steps to reproduce it. + +## Suggesting Features +If you want to suggest a new feature or improvement, first check the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to ensure a similar suggestion hasn't already been made. Use the "Feature request" template to provide a detailed description of your suggestion. + +## Reporting Vulnerabilities +If you identify a security concern, please use the "Report a security vulnerability" template on the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to share the details. This report will only be viewable to repository maintainers. You will be credited if the advisory is published. + +## Questions for Users +If you have questions, please utilize [StackOverflow](https://stackoverflow.com/) or the [GitHub Discussions page](https://github.com/sashabaranov/go-openai/discussions). + +## Contributing Code +There might already be a similar pull requests submitted! Please search for [pull requests](https://github.com/sashabaranov/go-openai/pulls) before creating one. + +### Requirements for Merging a Pull Request + +The requirements to accept a pull request are as follows: + +- Features not provided by the OpenAI API will not be accepted. +- The functionality of the feature must match that of the official OpenAI API. +- All pull requests should be written in Go according to common conventions, formatted with `goimports`, and free of warnings from tools like `golangci-lint`. +- Include tests and ensure all tests pass. +- Maintain test coverage without any reduction. +- All pull requests require approval from at least one Go OpenAI maintainer. + +**Note:** +The merging method for pull requests in this repository is squash merge. + +### Creating a Pull Request +- Fork the repository. +- Create a new branch and commit your changes. +- Push that branch to GitHub. +- Start a new Pull Request on GitHub. (Please use the pull request template to provide detailed information.) + +**Note:** +If your changes introduce breaking changes, please prefix your pull request title with "[BREAKING_CHANGES]". + +### Code Style +In this project, we adhere to the standard coding style of Go. Your code should maintain consistency with the rest of the codebase. To achieve this, please format your code using tools like `goimports` and resolve any syntax or style issues with `golangci-lint`. + +**Run goimports:** +``` +go install golang.org/x/tools/cmd/goimports@latest +``` + +``` +goimports -w . +``` + +**Run golangci-lint:** +``` +go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest +``` + +``` +golangci-lint run --out-format=github-actions +``` + +### Unit Test +Please create or update tests relevant to your changes. Ensure all tests run successfully to verify that your modifications do not adversely affect other functionalities. + +**Run test:** +``` +go test -v ./... +``` + +### Integration Test +Integration tests are requested against the production version of the OpenAI API. These tests will verify that the library is properly coded against the actual behavior of the API, and will fail upon any incompatible change in the API. + +**Notes:** +These tests send real network traffic to the OpenAI API and may reach rate limits. Temporary network problems may also cause the test to fail. + +**Run integration test:** +``` +OPENAI_TOKEN=XXX go test -v -tags=integration ./api_integration_test.go +``` + +If the `OPENAI_TOKEN` environment variable is not available, integration tests will be skipped. + +--- + +We wholeheartedly welcome your active participation. Let's build an amazing project together! diff --git a/backend/vendor/github.com/sashabaranov/go-openai/LICENSE b/backend/vendor/github.com/sashabaranov/go-openai/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/backend/vendor/github.com/sashabaranov/go-openai/README.md b/backend/vendor/github.com/sashabaranov/go-openai/README.md new file mode 100644 index 0000000..57d1d35 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/README.md @@ -0,0 +1,853 @@ +# Go OpenAI +[![Go Reference](https://pkg.go.dev/badge/github.com/sashabaranov/go-openai.svg)](https://pkg.go.dev/github.com/sashabaranov/go-openai) +[![Go Report Card](https://goreportcard.com/badge/github.com/sashabaranov/go-openai)](https://goreportcard.com/report/github.com/sashabaranov/go-openai) +[![codecov](https://codecov.io/gh/sashabaranov/go-openai/branch/master/graph/badge.svg?token=bCbIfHLIsW)](https://codecov.io/gh/sashabaranov/go-openai) + +This library provides unofficial Go clients for [OpenAI API](https://platform.openai.com/). We support: + +* ChatGPT 4o, o1 +* GPT-3, GPT-4 +* DALL·E 2, DALL·E 3 +* Whisper + +## Installation + +``` +go get github.com/sashabaranov/go-openai +``` +Currently, go-openai requires Go version 1.18 or greater. + + +## Usage + +### ChatGPT example usage: + +```go +package main + +import ( + "context" + "fmt" + openai "github.com/sashabaranov/go-openai" +) + +func main() { + client := openai.NewClient("your token") + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: "Hello!", + }, + }, + }, + ) + + if err != nil { + fmt.Printf("ChatCompletion error: %v\n", err) + return + } + + fmt.Println(resp.Choices[0].Message.Content) +} + +``` + +### Getting an OpenAI API Key: + +1. Visit the OpenAI website at [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys). +2. If you don't have an account, click on "Sign Up" to create one. If you do, click "Log In". +3. Once logged in, navigate to your API key management page. +4. Click on "Create new secret key". +5. Enter a name for your new key, then click "Create secret key". +6. Your new API key will be displayed. Use this key to interact with the OpenAI API. + +**Note:** Your API key is sensitive information. Do not share it with anyone. + +### Other examples: + +
+ChatGPT streaming completion + +```go +package main + +import ( + "context" + "errors" + "fmt" + "io" + openai "github.com/sashabaranov/go-openai" +) + +func main() { + c := openai.NewClient("your token") + ctx := context.Background() + + req := openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + MaxTokens: 20, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: "Lorem ipsum", + }, + }, + Stream: true, + } + stream, err := c.CreateChatCompletionStream(ctx, req) + if err != nil { + fmt.Printf("ChatCompletionStream error: %v\n", err) + return + } + defer stream.Close() + + fmt.Printf("Stream response: ") + for { + response, err := stream.Recv() + if errors.Is(err, io.EOF) { + fmt.Println("\nStream finished") + return + } + + if err != nil { + fmt.Printf("\nStream error: %v\n", err) + return + } + + fmt.Printf(response.Choices[0].Delta.Content) + } +} +``` +
+ +
+GPT-3 completion + +```go +package main + +import ( + "context" + "fmt" + openai "github.com/sashabaranov/go-openai" +) + +func main() { + c := openai.NewClient("your token") + ctx := context.Background() + + req := openai.CompletionRequest{ + Model: openai.GPT3Babbage002, + MaxTokens: 5, + Prompt: "Lorem ipsum", + } + resp, err := c.CreateCompletion(ctx, req) + if err != nil { + fmt.Printf("Completion error: %v\n", err) + return + } + fmt.Println(resp.Choices[0].Text) +} +``` +
+ +
+GPT-3 streaming completion + +```go +package main + +import ( + "errors" + "context" + "fmt" + "io" + openai "github.com/sashabaranov/go-openai" +) + +func main() { + c := openai.NewClient("your token") + ctx := context.Background() + + req := openai.CompletionRequest{ + Model: openai.GPT3Babbage002, + MaxTokens: 5, + Prompt: "Lorem ipsum", + Stream: true, + } + stream, err := c.CreateCompletionStream(ctx, req) + if err != nil { + fmt.Printf("CompletionStream error: %v\n", err) + return + } + defer stream.Close() + + for { + response, err := stream.Recv() + if errors.Is(err, io.EOF) { + fmt.Println("Stream finished") + return + } + + if err != nil { + fmt.Printf("Stream error: %v\n", err) + return + } + + + fmt.Printf("Stream response: %v\n", response) + } +} +``` +
+ +
+Audio Speech-To-Text + +```go +package main + +import ( + "context" + "fmt" + + openai "github.com/sashabaranov/go-openai" +) + +func main() { + c := openai.NewClient("your token") + ctx := context.Background() + + req := openai.AudioRequest{ + Model: openai.Whisper1, + FilePath: "recording.mp3", + } + resp, err := c.CreateTranscription(ctx, req) + if err != nil { + fmt.Printf("Transcription error: %v\n", err) + return + } + fmt.Println(resp.Text) +} +``` +
+ +
+Audio Captions + +```go +package main + +import ( + "context" + "fmt" + "os" + + openai "github.com/sashabaranov/go-openai" +) + +func main() { + c := openai.NewClient(os.Getenv("OPENAI_KEY")) + + req := openai.AudioRequest{ + Model: openai.Whisper1, + FilePath: os.Args[1], + Format: openai.AudioResponseFormatSRT, + } + resp, err := c.CreateTranscription(context.Background(), req) + if err != nil { + fmt.Printf("Transcription error: %v\n", err) + return + } + f, err := os.Create(os.Args[1] + ".srt") + if err != nil { + fmt.Printf("Could not open file: %v\n", err) + return + } + defer f.Close() + if _, err := f.WriteString(resp.Text); err != nil { + fmt.Printf("Error writing to file: %v\n", err) + return + } +} +``` +
+ +
+DALL-E 2 image generation + +```go +package main + +import ( + "bytes" + "context" + "encoding/base64" + "fmt" + openai "github.com/sashabaranov/go-openai" + "image/png" + "os" +) + +func main() { + c := openai.NewClient("your token") + ctx := context.Background() + + // Sample image by link + reqUrl := openai.ImageRequest{ + Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail", + Size: openai.CreateImageSize256x256, + ResponseFormat: openai.CreateImageResponseFormatURL, + N: 1, + } + + respUrl, err := c.CreateImage(ctx, reqUrl) + if err != nil { + fmt.Printf("Image creation error: %v\n", err) + return + } + fmt.Println(respUrl.Data[0].URL) + + // Example image as base64 + reqBase64 := openai.ImageRequest{ + Prompt: "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine", + Size: openai.CreateImageSize256x256, + ResponseFormat: openai.CreateImageResponseFormatB64JSON, + N: 1, + } + + respBase64, err := c.CreateImage(ctx, reqBase64) + if err != nil { + fmt.Printf("Image creation error: %v\n", err) + return + } + + imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON) + if err != nil { + fmt.Printf("Base64 decode error: %v\n", err) + return + } + + r := bytes.NewReader(imgBytes) + imgData, err := png.Decode(r) + if err != nil { + fmt.Printf("PNG decode error: %v\n", err) + return + } + + file, err := os.Create("example.png") + if err != nil { + fmt.Printf("File creation error: %v\n", err) + return + } + defer file.Close() + + if err := png.Encode(file, imgData); err != nil { + fmt.Printf("PNG encode error: %v\n", err) + return + } + + fmt.Println("The image was saved as example.png") +} + +``` +
+ +
+Configuring proxy + +```go +config := openai.DefaultConfig("token") +proxyUrl, err := url.Parse("http://localhost:{port}") +if err != nil { + panic(err) +} +transport := &http.Transport{ + Proxy: http.ProxyURL(proxyUrl), +} +config.HTTPClient = &http.Client{ + Transport: transport, +} + +c := openai.NewClientWithConfig(config) +``` + +See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig +
+ +
+ChatGPT support context + +```go +package main + +import ( + "bufio" + "context" + "fmt" + "os" + "strings" + + "github.com/sashabaranov/go-openai" +) + +func main() { + client := openai.NewClient("your token") + messages := make([]openai.ChatCompletionMessage, 0) + reader := bufio.NewReader(os.Stdin) + fmt.Println("Conversation") + fmt.Println("---------------------") + + for { + fmt.Print("-> ") + text, _ := reader.ReadString('\n') + // convert CRLF to LF + text = strings.Replace(text, "\n", "", -1) + messages = append(messages, openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleUser, + Content: text, + }) + + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: messages, + }, + ) + + if err != nil { + fmt.Printf("ChatCompletion error: %v\n", err) + continue + } + + content := resp.Choices[0].Message.Content + messages = append(messages, openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleAssistant, + Content: content, + }) + fmt.Println(content) + } +} +``` +
+ +
+Azure OpenAI ChatGPT + +```go +package main + +import ( + "context" + "fmt" + + openai "github.com/sashabaranov/go-openai" +) + +func main() { + config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") + // If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function + // config.AzureModelMapperFunc = func(model string) string { + // azureModelMapping := map[string]string{ + // "gpt-3.5-turbo": "your gpt-3.5-turbo deployment name", + // } + // return azureModelMapping[model] + // } + + client := openai.NewClientWithConfig(config) + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: "Hello Azure OpenAI!", + }, + }, + }, + ) + if err != nil { + fmt.Printf("ChatCompletion error: %v\n", err) + return + } + + fmt.Println(resp.Choices[0].Message.Content) +} + +``` +
+ +
+Embedding Semantic Similarity + +```go +package main + +import ( + "context" + "log" + openai "github.com/sashabaranov/go-openai" + +) + +func main() { + client := openai.NewClient("your-token") + + // Create an EmbeddingRequest for the user query + queryReq := openai.EmbeddingRequest{ + Input: []string{"How many chucks would a woodchuck chuck"}, + Model: openai.AdaEmbeddingV2, + } + + // Create an embedding for the user query + queryResponse, err := client.CreateEmbeddings(context.Background(), queryReq) + if err != nil { + log.Fatal("Error creating query embedding:", err) + } + + // Create an EmbeddingRequest for the target text + targetReq := openai.EmbeddingRequest{ + Input: []string{"How many chucks would a woodchuck chuck if the woodchuck could chuck wood"}, + Model: openai.AdaEmbeddingV2, + } + + // Create an embedding for the target text + targetResponse, err := client.CreateEmbeddings(context.Background(), targetReq) + if err != nil { + log.Fatal("Error creating target embedding:", err) + } + + // Now that we have the embeddings for the user query and the target text, we + // can calculate their similarity. + queryEmbedding := queryResponse.Data[0] + targetEmbedding := targetResponse.Data[0] + + similarity, err := queryEmbedding.DotProduct(&targetEmbedding) + if err != nil { + log.Fatal("Error calculating dot product:", err) + } + + log.Printf("The similarity score between the query and the target is %f", similarity) +} + +``` +
+ +
+Azure OpenAI Embeddings + +```go +package main + +import ( + "context" + "fmt" + + openai "github.com/sashabaranov/go-openai" +) + +func main() { + + config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") + config.APIVersion = "2023-05-15" // optional update to latest API version + + //If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function + //config.AzureModelMapperFunc = func(model string) string { + // azureModelMapping := map[string]string{ + // "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name", + // } + // return azureModelMapping[model] + //} + + input := "Text to vectorize" + + client := openai.NewClientWithConfig(config) + resp, err := client.CreateEmbeddings( + context.Background(), + openai.EmbeddingRequest{ + Input: []string{input}, + Model: openai.AdaEmbeddingV2, + }) + + if err != nil { + fmt.Printf("CreateEmbeddings error: %v\n", err) + return + } + + vectors := resp.Data[0].Embedding // []float32 with 1536 dimensions + + fmt.Println(vectors[:10], "...", vectors[len(vectors)-10:]) +} +``` +
+ +
+JSON Schema for function calling + +It is now possible for chat completion to choose to call a function for more information ([see developer docs here](https://platform.openai.com/docs/guides/gpt/function-calling)). + +In order to describe the type of functions that can be called, a JSON schema must be provided. Many JSON schema libraries exist and are more advanced than what we can offer in this library, however we have included a simple `jsonschema` package for those who want to use this feature without formatting their own JSON schema payload. + +The developer documents give this JSON schema definition as an example: + +```json +{ + "name":"get_current_weather", + "description":"Get the current weather in a given location", + "parameters":{ + "type":"object", + "properties":{ + "location":{ + "type":"string", + "description":"The city and state, e.g. San Francisco, CA" + }, + "unit":{ + "type":"string", + "enum":[ + "celsius", + "fahrenheit" + ] + } + }, + "required":[ + "location" + ] + } +} +``` + +Using the `jsonschema` package, this schema could be created using structs as such: + +```go +FunctionDefinition{ + Name: "get_current_weather", + Parameters: jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "location": { + Type: jsonschema.String, + Description: "The city and state, e.g. San Francisco, CA", + }, + "unit": { + Type: jsonschema.String, + Enum: []string{"celsius", "fahrenheit"}, + }, + }, + Required: []string{"location"}, + }, +} +``` + +The `Parameters` field of a `FunctionDefinition` can accept either of the above styles, or even a nested struct from another library (as long as it can be marshalled into JSON). +
+ +
+Error handling + +Open-AI maintains clear documentation on how to [handle API errors](https://platform.openai.com/docs/guides/error-codes/api-errors) + +example: +``` +e := &openai.APIError{} +if errors.As(err, &e) { + switch e.HTTPStatusCode { + case 401: + // invalid auth or key (do not retry) + case 429: + // rate limiting or engine overload (wait and retry) + case 500: + // openai server error (retry) + default: + // unhandled + } +} + +``` +
+ +
+Fine Tune Model + +```go +package main + +import ( + "context" + "fmt" + "github.com/sashabaranov/go-openai" +) + +func main() { + client := openai.NewClient("your token") + ctx := context.Background() + + // create a .jsonl file with your training data for conversational model + // {"prompt": "", "completion": ""} + // {"prompt": "", "completion": ""} + // {"prompt": "", "completion": ""} + + // chat models are trained using the following file format: + // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content": "Paris, as if everyone doesn't know that already."}]} + // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "Who wrote 'Romeo and Juliet'?"}, {"role": "assistant", "content": "Oh, just some guy named William Shakespeare. Ever heard of him?"}]} + // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "How far is the Moon from Earth?"}, {"role": "assistant", "content": "Around 384,400 kilometers. Give or take a few, like that really matters."}]} + + // you can use openai cli tool to validate the data + // For more info - https://platform.openai.com/docs/guides/fine-tuning + + file, err := client.CreateFile(ctx, openai.FileRequest{ + FilePath: "training_prepared.jsonl", + Purpose: "fine-tune", + }) + if err != nil { + fmt.Printf("Upload JSONL file error: %v\n", err) + return + } + + // create a fine tuning job + // Streams events until the job is done (this often takes minutes, but can take hours if there are many jobs in the queue or your dataset is large) + // use below get method to know the status of your model + fineTuningJob, err := client.CreateFineTuningJob(ctx, openai.FineTuningJobRequest{ + TrainingFile: file.ID, + Model: "davinci-002", // gpt-3.5-turbo-0613, babbage-002. + }) + if err != nil { + fmt.Printf("Creating new fine tune model error: %v\n", err) + return + } + + fineTuningJob, err = client.RetrieveFineTuningJob(ctx, fineTuningJob.ID) + if err != nil { + fmt.Printf("Getting fine tune model error: %v\n", err) + return + } + fmt.Println(fineTuningJob.FineTunedModel) + + // once the status of fineTuningJob is `succeeded`, you can use your fine tune model in Completion Request or Chat Completion Request + + // resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{ + // Model: fineTuningJob.FineTunedModel, + // Prompt: "your prompt", + // }) + // if err != nil { + // fmt.Printf("Create completion error %v\n", err) + // return + // } + // + // fmt.Println(resp.Choices[0].Text) +} +``` +
+ +
+Structured Outputs + +```go +package main + +import ( + "context" + "fmt" + "log" + + "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/jsonschema" +) + +func main() { + client := openai.NewClient("your token") + ctx := context.Background() + + type Result struct { + Steps []struct { + Explanation string `json:"explanation"` + Output string `json:"output"` + } `json:"steps"` + FinalAnswer string `json:"final_answer"` + } + var result Result + schema, err := jsonschema.GenerateSchemaForType(result) + if err != nil { + log.Fatalf("GenerateSchemaForType error: %v", err) + } + resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ + Model: openai.GPT4oMini, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleSystem, + Content: "You are a helpful math tutor. Guide the user through the solution step by step.", + }, + { + Role: openai.ChatMessageRoleUser, + Content: "how can I solve 8x + 7 = -23", + }, + }, + ResponseFormat: &openai.ChatCompletionResponseFormat{ + Type: openai.ChatCompletionResponseFormatTypeJSONSchema, + JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{ + Name: "math_reasoning", + Schema: schema, + Strict: true, + }, + }, + }) + if err != nil { + log.Fatalf("CreateChatCompletion error: %v", err) + } + err = schema.Unmarshal(resp.Choices[0].Message.Content, &result) + if err != nil { + log.Fatalf("Unmarshal schema error: %v", err) + } + fmt.Println(result) +} +``` +
+See the `examples/` folder for more. + +## Frequently Asked Questions + +### Why don't we get the same answer when specifying a temperature field of 0 and asking the same question? + +Even when specifying a temperature field of 0, it doesn't guarantee that you'll always get the same response. Several factors come into play. + +1. Go OpenAI Behavior: When you specify a temperature field of 0 in Go OpenAI, the omitempty tag causes that field to be removed from the request. Consequently, the OpenAI API applies the default value of 1. +2. Token Count for Input/Output: If there's a large number of tokens in the input and output, setting the temperature to 0 can still result in non-deterministic behavior. In particular, when using around 32k tokens, the likelihood of non-deterministic behavior becomes highest even with a temperature of 0. + +Due to the factors mentioned above, different answers may be returned even for the same question. + +**Workarounds:** +1. As of November 2023, use [the new `seed` parameter](https://platform.openai.com/docs/guides/text-generation/reproducible-outputs) in conjunction with the `system_fingerprint` response field, alongside Temperature management. +2. Try using `math.SmallestNonzeroFloat32`: By specifying `math.SmallestNonzeroFloat32` in the temperature field instead of 0, you can mimic the behavior of setting it to 0. +3. Limiting Token Count: By limiting the number of tokens in the input and output and especially avoiding large requests close to 32k tokens, you can reduce the risk of non-deterministic behavior. + +By adopting these strategies, you can expect more consistent results. + +**Related Issues:** +[omitempty option of request struct will generate incorrect request when parameter is 0.](https://github.com/sashabaranov/go-openai/issues/9) + +### Does Go OpenAI provide a method to count tokens? + +No, Go OpenAI does not offer a feature to count tokens, and there are no plans to provide such a feature in the future. However, if there's a way to implement a token counting feature with zero dependencies, it might be possible to merge that feature into Go OpenAI. Otherwise, it would be more appropriate to implement it in a dedicated library or repository. + +For counting tokens, you might find the following links helpful: +- [Counting Tokens For Chat API Calls](https://github.com/pkoukk/tiktoken-go#counting-tokens-for-chat-api-calls) +- [How to count tokens with tiktoken](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb) + +**Related Issues:** +[Is it possible to join the implementation of GPT3 Tokenizer](https://github.com/sashabaranov/go-openai/issues/62) + +## Contributing + +By following [Contributing Guidelines](https://github.com/sashabaranov/go-openai/blob/master/CONTRIBUTING.md), we hope to ensure that your contributions are made smoothly and efficiently. + +## Thank you + +We want to take a moment to express our deepest gratitude to the [contributors](https://github.com/sashabaranov/go-openai/graphs/contributors) and sponsors of this project: +- [Carson Kahn](https://carsonkahn.com) of [Spindle AI](https://spindleai.com) + +To all of you: thank you. You've helped us achieve more than we ever imagined possible. Can't wait to see where we go next, together! diff --git a/backend/vendor/github.com/sashabaranov/go-openai/assistant.go b/backend/vendor/github.com/sashabaranov/go-openai/assistant.go new file mode 100644 index 0000000..8aab5bc --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/assistant.go @@ -0,0 +1,325 @@ +package openai + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +const ( + assistantsSuffix = "/assistants" + assistantsFilesSuffix = "/files" +) + +type Assistant struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Name *string `json:"name,omitempty"` + Description *string `json:"description,omitempty"` + Model string `json:"model"` + Instructions *string `json:"instructions,omitempty"` + Tools []AssistantTool `json:"tools"` + ToolResources *AssistantToolResource `json:"tool_resources,omitempty"` + FileIDs []string `json:"file_ids,omitempty"` // Deprecated in v2 + Metadata map[string]any `json:"metadata,omitempty"` + Temperature *float32 `json:"temperature,omitempty"` + TopP *float32 `json:"top_p,omitempty"` + ResponseFormat any `json:"response_format,omitempty"` + + httpHeader +} + +type AssistantToolType string + +const ( + AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter" + AssistantToolTypeRetrieval AssistantToolType = "retrieval" + AssistantToolTypeFunction AssistantToolType = "function" + AssistantToolTypeFileSearch AssistantToolType = "file_search" +) + +type AssistantTool struct { + Type AssistantToolType `json:"type"` + Function *FunctionDefinition `json:"function,omitempty"` +} + +type AssistantToolFileSearch struct { + VectorStoreIDs []string `json:"vector_store_ids"` +} + +type AssistantToolCodeInterpreter struct { + FileIDs []string `json:"file_ids"` +} + +type AssistantToolResource struct { + FileSearch *AssistantToolFileSearch `json:"file_search,omitempty"` + CodeInterpreter *AssistantToolCodeInterpreter `json:"code_interpreter,omitempty"` +} + +// AssistantRequest provides the assistant request parameters. +// When modifying the tools the API functions as the following: +// If Tools is undefined, no changes are made to the Assistant's tools. +// If Tools is empty slice it will effectively delete all of the Assistant's tools. +// If Tools is populated, it will replace all of the existing Assistant's tools with the provided tools. +type AssistantRequest struct { + Model string `json:"model"` + Name *string `json:"name,omitempty"` + Description *string `json:"description,omitempty"` + Instructions *string `json:"instructions,omitempty"` + Tools []AssistantTool `json:"-"` + FileIDs []string `json:"file_ids,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` + ToolResources *AssistantToolResource `json:"tool_resources,omitempty"` + ResponseFormat any `json:"response_format,omitempty"` + Temperature *float32 `json:"temperature,omitempty"` + TopP *float32 `json:"top_p,omitempty"` +} + +// MarshalJSON provides a custom marshaller for the assistant request to handle the API use cases +// If Tools is nil, the field is omitted from the JSON. +// If Tools is an empty slice, it's included in the JSON as an empty array ([]). +// If Tools is populated, it's included in the JSON with the elements. +func (a AssistantRequest) MarshalJSON() ([]byte, error) { + type Alias AssistantRequest + assistantAlias := &struct { + Tools *[]AssistantTool `json:"tools,omitempty"` + *Alias + }{ + Alias: (*Alias)(&a), + } + + if a.Tools != nil { + assistantAlias.Tools = &a.Tools + } + + return json.Marshal(assistantAlias) +} + +// AssistantsList is a list of assistants. +type AssistantsList struct { + Assistants []Assistant `json:"data"` + LastID *string `json:"last_id"` + FirstID *string `json:"first_id"` + HasMore bool `json:"has_more"` + httpHeader +} + +type AssistantDeleteResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +type AssistantFile struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + AssistantID string `json:"assistant_id"` + + httpHeader +} + +type AssistantFileRequest struct { + FileID string `json:"file_id"` +} + +type AssistantFilesList struct { + AssistantFiles []AssistantFile `json:"data"` + + httpHeader +} + +// CreateAssistant creates a new assistant. +func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error) { + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveAssistant retrieves an assistant. +func (c *Client) RetrieveAssistant( + ctx context.Context, + assistantID string, +) (response Assistant, err error) { + urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ModifyAssistant modifies an assistant. +func (c *Client) ModifyAssistant( + ctx context.Context, + assistantID string, + request AssistantRequest, +) (response Assistant, err error) { + urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// DeleteAssistant deletes an assistant. +func (c *Client) DeleteAssistant( + ctx context.Context, + assistantID string, +) (response AssistantDeleteResponse, err error) { + urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ListAssistants Lists the currently available assistants. +func (c *Client) ListAssistants( + ctx context.Context, + limit *int, + order *string, + after *string, + before *string, +) (response AssistantsList, err error) { + urlValues := url.Values{} + if limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *limit)) + } + if order != nil { + urlValues.Add("order", *order) + } + if after != nil { + urlValues.Add("after", *after) + } + if before != nil { + urlValues.Add("before", *before) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// CreateAssistantFile creates a new assistant file. +func (c *Client) CreateAssistantFile( + ctx context.Context, + assistantID string, + request AssistantFileRequest, +) (response AssistantFile, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveAssistantFile retrieves an assistant file. +func (c *Client) RetrieveAssistantFile( + ctx context.Context, + assistantID string, + fileID string, +) (response AssistantFile, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// DeleteAssistantFile deletes an existing file. +func (c *Client) DeleteAssistantFile( + ctx context.Context, + assistantID string, + fileID string, +) (err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID) + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, nil) + return +} + +// ListAssistantFiles Lists the currently available files for an assistant. +func (c *Client) ListAssistantFiles( + ctx context.Context, + assistantID string, + limit *int, + order *string, + after *string, + before *string, +) (response AssistantFilesList, err error) { + urlValues := url.Values{} + if limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *limit)) + } + if order != nil { + urlValues.Add("order", *order) + } + if after != nil { + urlValues.Add("after", *after) + } + if before != nil { + urlValues.Add("before", *before) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/audio.go b/backend/vendor/github.com/sashabaranov/go-openai/audio.go new file mode 100644 index 0000000..f321f93 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/audio.go @@ -0,0 +1,234 @@ +package openai + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + "os" + + utils "github.com/sashabaranov/go-openai/internal" +) + +// Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI. +const ( + Whisper1 = "whisper-1" +) + +// Response formats; Whisper uses AudioResponseFormatJSON by default. +type AudioResponseFormat string + +const ( + AudioResponseFormatJSON AudioResponseFormat = "json" + AudioResponseFormatText AudioResponseFormat = "text" + AudioResponseFormatSRT AudioResponseFormat = "srt" + AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" + AudioResponseFormatVTT AudioResponseFormat = "vtt" +) + +type TranscriptionTimestampGranularity string + +const ( + TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word" + TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment" +) + +// AudioRequest represents a request structure for audio API. +type AudioRequest struct { + Model string + + // FilePath is either an existing file in your filesystem or a filename representing the contents of Reader. + FilePath string + + // Reader is an optional io.Reader when you do not want to use an existing file. + Reader io.Reader + + Prompt string + Temperature float32 + Language string // Only for transcription. + Format AudioResponseFormat + TimestampGranularities []TranscriptionTimestampGranularity // Only for transcription. +} + +// AudioResponse represents a response structure for audio API. +type AudioResponse struct { + Task string `json:"task"` + Language string `json:"language"` + Duration float64 `json:"duration"` + Segments []struct { + ID int `json:"id"` + Seek int `json:"seek"` + Start float64 `json:"start"` + End float64 `json:"end"` + Text string `json:"text"` + Tokens []int `json:"tokens"` + Temperature float64 `json:"temperature"` + AvgLogprob float64 `json:"avg_logprob"` + CompressionRatio float64 `json:"compression_ratio"` + NoSpeechProb float64 `json:"no_speech_prob"` + Transient bool `json:"transient"` + } `json:"segments"` + Words []struct { + Word string `json:"word"` + Start float64 `json:"start"` + End float64 `json:"end"` + } `json:"words"` + Text string `json:"text"` + + httpHeader +} + +type audioTextResponse struct { + Text string `json:"text"` + + httpHeader +} + +func (r *audioTextResponse) ToAudioResponse() AudioResponse { + return AudioResponse{ + Text: r.Text, + httpHeader: r.httpHeader, + } +} + +// CreateTranscription — API call to create a transcription. Returns transcribed text. +func (c *Client) CreateTranscription( + ctx context.Context, + request AudioRequest, +) (response AudioResponse, err error) { + return c.callAudioAPI(ctx, request, "transcriptions") +} + +// CreateTranslation — API call to translate audio into English. +func (c *Client) CreateTranslation( + ctx context.Context, + request AudioRequest, +) (response AudioResponse, err error) { + return c.callAudioAPI(ctx, request, "translations") +} + +// callAudioAPI — API call to an audio endpoint. +func (c *Client) callAudioAPI( + ctx context.Context, + request AudioRequest, + endpointSuffix string, +) (response AudioResponse, err error) { + var formBody bytes.Buffer + builder := c.createFormBuilder(&formBody) + + if err = audioMultipartForm(request, builder); err != nil { + return AudioResponse{}, err + } + + urlSuffix := fmt.Sprintf("/audio/%s", endpointSuffix) + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(&formBody), + withContentType(builder.FormDataContentType()), + ) + if err != nil { + return AudioResponse{}, err + } + + if request.HasJSONResponse() { + err = c.sendRequest(req, &response) + } else { + var textResponse audioTextResponse + err = c.sendRequest(req, &textResponse) + response = textResponse.ToAudioResponse() + } + if err != nil { + return AudioResponse{}, err + } + return +} + +// HasJSONResponse returns true if the response format is JSON. +func (r AudioRequest) HasJSONResponse() bool { + return r.Format == "" || r.Format == AudioResponseFormatJSON || r.Format == AudioResponseFormatVerboseJSON +} + +// audioMultipartForm creates a form with audio file contents and the name of the model to use for +// audio processing. +func audioMultipartForm(request AudioRequest, b utils.FormBuilder) error { + err := createFileField(request, b) + if err != nil { + return err + } + + err = b.WriteField("model", request.Model) + if err != nil { + return fmt.Errorf("writing model name: %w", err) + } + + // Create a form field for the prompt (if provided) + if request.Prompt != "" { + err = b.WriteField("prompt", request.Prompt) + if err != nil { + return fmt.Errorf("writing prompt: %w", err) + } + } + + // Create a form field for the format (if provided) + if request.Format != "" { + err = b.WriteField("response_format", string(request.Format)) + if err != nil { + return fmt.Errorf("writing format: %w", err) + } + } + + // Create a form field for the temperature (if provided) + if request.Temperature != 0 { + err = b.WriteField("temperature", fmt.Sprintf("%.2f", request.Temperature)) + if err != nil { + return fmt.Errorf("writing temperature: %w", err) + } + } + + // Create a form field for the language (if provided) + if request.Language != "" { + err = b.WriteField("language", request.Language) + if err != nil { + return fmt.Errorf("writing language: %w", err) + } + } + + if len(request.TimestampGranularities) > 0 { + for _, tg := range request.TimestampGranularities { + err = b.WriteField("timestamp_granularities[]", string(tg)) + if err != nil { + return fmt.Errorf("writing timestamp_granularities[]: %w", err) + } + } + } + + // Close the multipart writer + return b.Close() +} + +// createFileField creates the "file" form field from either an existing file or by using the reader. +func createFileField(request AudioRequest, b utils.FormBuilder) error { + if request.Reader != nil { + err := b.CreateFormFileReader("file", request.Reader, request.FilePath) + if err != nil { + return fmt.Errorf("creating form using reader: %w", err) + } + return nil + } + + f, err := os.Open(request.FilePath) + if err != nil { + return fmt.Errorf("opening audio file: %w", err) + } + defer f.Close() + + err = b.CreateFormFile("file", f) + if err != nil { + return fmt.Errorf("creating form file: %w", err) + } + + return nil +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/batch.go b/backend/vendor/github.com/sashabaranov/go-openai/batch.go new file mode 100644 index 0000000..3c1a9d0 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/batch.go @@ -0,0 +1,271 @@ +package openai + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +const batchesSuffix = "/batches" + +type BatchEndpoint string + +const ( + BatchEndpointChatCompletions BatchEndpoint = "/v1/chat/completions" + BatchEndpointCompletions BatchEndpoint = "/v1/completions" + BatchEndpointEmbeddings BatchEndpoint = "/v1/embeddings" +) + +type BatchLineItem interface { + MarshalBatchLineItem() []byte +} + +type BatchChatCompletionRequest struct { + CustomID string `json:"custom_id"` + Body ChatCompletionRequest `json:"body"` + Method string `json:"method"` + URL BatchEndpoint `json:"url"` +} + +func (r BatchChatCompletionRequest) MarshalBatchLineItem() []byte { + marshal, _ := json.Marshal(r) + return marshal +} + +type BatchCompletionRequest struct { + CustomID string `json:"custom_id"` + Body CompletionRequest `json:"body"` + Method string `json:"method"` + URL BatchEndpoint `json:"url"` +} + +func (r BatchCompletionRequest) MarshalBatchLineItem() []byte { + marshal, _ := json.Marshal(r) + return marshal +} + +type BatchEmbeddingRequest struct { + CustomID string `json:"custom_id"` + Body EmbeddingRequest `json:"body"` + Method string `json:"method"` + URL BatchEndpoint `json:"url"` +} + +func (r BatchEmbeddingRequest) MarshalBatchLineItem() []byte { + marshal, _ := json.Marshal(r) + return marshal +} + +type Batch struct { + ID string `json:"id"` + Object string `json:"object"` + Endpoint BatchEndpoint `json:"endpoint"` + Errors *struct { + Object string `json:"object,omitempty"` + Data []struct { + Code string `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Param *string `json:"param,omitempty"` + Line *int `json:"line,omitempty"` + } `json:"data"` + } `json:"errors"` + InputFileID string `json:"input_file_id"` + CompletionWindow string `json:"completion_window"` + Status string `json:"status"` + OutputFileID *string `json:"output_file_id"` + ErrorFileID *string `json:"error_file_id"` + CreatedAt int `json:"created_at"` + InProgressAt *int `json:"in_progress_at"` + ExpiresAt *int `json:"expires_at"` + FinalizingAt *int `json:"finalizing_at"` + CompletedAt *int `json:"completed_at"` + FailedAt *int `json:"failed_at"` + ExpiredAt *int `json:"expired_at"` + CancellingAt *int `json:"cancelling_at"` + CancelledAt *int `json:"cancelled_at"` + RequestCounts BatchRequestCounts `json:"request_counts"` + Metadata map[string]any `json:"metadata"` +} + +type BatchRequestCounts struct { + Total int `json:"total"` + Completed int `json:"completed"` + Failed int `json:"failed"` +} + +type CreateBatchRequest struct { + InputFileID string `json:"input_file_id"` + Endpoint BatchEndpoint `json:"endpoint"` + CompletionWindow string `json:"completion_window"` + Metadata map[string]any `json:"metadata"` +} + +type BatchResponse struct { + httpHeader + Batch +} + +// CreateBatch — API call to Create batch. +func (c *Client) CreateBatch( + ctx context.Context, + request CreateBatchRequest, +) (response BatchResponse, err error) { + if request.CompletionWindow == "" { + request.CompletionWindow = "24h" + } + + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(batchesSuffix), withBody(request)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +type UploadBatchFileRequest struct { + FileName string + Lines []BatchLineItem +} + +func (r *UploadBatchFileRequest) MarshalJSONL() []byte { + buff := bytes.Buffer{} + for i, line := range r.Lines { + if i != 0 { + buff.Write([]byte("\n")) + } + buff.Write(line.MarshalBatchLineItem()) + } + return buff.Bytes() +} + +func (r *UploadBatchFileRequest) AddChatCompletion(customerID string, body ChatCompletionRequest) { + r.Lines = append(r.Lines, BatchChatCompletionRequest{ + CustomID: customerID, + Body: body, + Method: "POST", + URL: BatchEndpointChatCompletions, + }) +} + +func (r *UploadBatchFileRequest) AddCompletion(customerID string, body CompletionRequest) { + r.Lines = append(r.Lines, BatchCompletionRequest{ + CustomID: customerID, + Body: body, + Method: "POST", + URL: BatchEndpointCompletions, + }) +} + +func (r *UploadBatchFileRequest) AddEmbedding(customerID string, body EmbeddingRequest) { + r.Lines = append(r.Lines, BatchEmbeddingRequest{ + CustomID: customerID, + Body: body, + Method: "POST", + URL: BatchEndpointEmbeddings, + }) +} + +// UploadBatchFile — upload batch file. +func (c *Client) UploadBatchFile(ctx context.Context, request UploadBatchFileRequest) (File, error) { + if request.FileName == "" { + request.FileName = "@batchinput.jsonl" + } + return c.CreateFileBytes(ctx, FileBytesRequest{ + Name: request.FileName, + Bytes: request.MarshalJSONL(), + Purpose: PurposeBatch, + }) +} + +type CreateBatchWithUploadFileRequest struct { + Endpoint BatchEndpoint `json:"endpoint"` + CompletionWindow string `json:"completion_window"` + Metadata map[string]any `json:"metadata"` + UploadBatchFileRequest +} + +// CreateBatchWithUploadFile — API call to Create batch with upload file. +func (c *Client) CreateBatchWithUploadFile( + ctx context.Context, + request CreateBatchWithUploadFileRequest, +) (response BatchResponse, err error) { + var file File + file, err = c.UploadBatchFile(ctx, UploadBatchFileRequest{ + FileName: request.FileName, + Lines: request.Lines, + }) + if err != nil { + return + } + return c.CreateBatch(ctx, CreateBatchRequest{ + InputFileID: file.ID, + Endpoint: request.Endpoint, + CompletionWindow: request.CompletionWindow, + Metadata: request.Metadata, + }) +} + +// RetrieveBatch — API call to Retrieve batch. +func (c *Client) RetrieveBatch( + ctx context.Context, + batchID string, +) (response BatchResponse, err error) { + urlSuffix := fmt.Sprintf("%s/%s", batchesSuffix, batchID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + err = c.sendRequest(req, &response) + return +} + +// CancelBatch — API call to Cancel batch. +func (c *Client) CancelBatch( + ctx context.Context, + batchID string, +) (response BatchResponse, err error) { + urlSuffix := fmt.Sprintf("%s/%s/cancel", batchesSuffix, batchID) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix)) + if err != nil { + return + } + err = c.sendRequest(req, &response) + return +} + +type ListBatchResponse struct { + httpHeader + Object string `json:"object"` + Data []Batch `json:"data"` + FirstID string `json:"first_id"` + LastID string `json:"last_id"` + HasMore bool `json:"has_more"` +} + +// ListBatch API call to List batch. +func (c *Client) ListBatch(ctx context.Context, after *string, limit *int) (response ListBatchResponse, err error) { + urlValues := url.Values{} + if limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *limit)) + } + if after != nil { + urlValues.Add("after", *after) + } + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s%s", batchesSuffix, encodedValues) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/chat.go b/backend/vendor/github.com/sashabaranov/go-openai/chat.go new file mode 100644 index 0000000..995860c --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/chat.go @@ -0,0 +1,413 @@ +package openai + +import ( + "context" + "encoding/json" + "errors" + "net/http" +) + +// Chat message role defined by the OpenAI API. +const ( + ChatMessageRoleSystem = "system" + ChatMessageRoleUser = "user" + ChatMessageRoleAssistant = "assistant" + ChatMessageRoleFunction = "function" + ChatMessageRoleTool = "tool" + ChatMessageRoleDeveloper = "developer" +) + +const chatCompletionsSuffix = "/chat/completions" + +var ( + ErrChatCompletionInvalidModel = errors.New("this model is not supported with this method, please use CreateCompletion client method instead") //nolint:lll + ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll + ErrContentFieldsMisused = errors.New("can't use both Content and MultiContent properties simultaneously") +) + +type Hate struct { + Filtered bool `json:"filtered"` + Severity string `json:"severity,omitempty"` +} +type SelfHarm struct { + Filtered bool `json:"filtered"` + Severity string `json:"severity,omitempty"` +} +type Sexual struct { + Filtered bool `json:"filtered"` + Severity string `json:"severity,omitempty"` +} +type Violence struct { + Filtered bool `json:"filtered"` + Severity string `json:"severity,omitempty"` +} + +type JailBreak struct { + Filtered bool `json:"filtered"` + Detected bool `json:"detected"` +} + +type Profanity struct { + Filtered bool `json:"filtered"` + Detected bool `json:"detected"` +} + +type ContentFilterResults struct { + Hate Hate `json:"hate,omitempty"` + SelfHarm SelfHarm `json:"self_harm,omitempty"` + Sexual Sexual `json:"sexual,omitempty"` + Violence Violence `json:"violence,omitempty"` + JailBreak JailBreak `json:"jailbreak,omitempty"` + Profanity Profanity `json:"profanity,omitempty"` +} + +type PromptAnnotation struct { + PromptIndex int `json:"prompt_index,omitempty"` + ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` +} + +type ImageURLDetail string + +const ( + ImageURLDetailHigh ImageURLDetail = "high" + ImageURLDetailLow ImageURLDetail = "low" + ImageURLDetailAuto ImageURLDetail = "auto" +) + +type ChatMessageImageURL struct { + URL string `json:"url,omitempty"` + Detail ImageURLDetail `json:"detail,omitempty"` +} + +type ChatMessagePartType string + +const ( + ChatMessagePartTypeText ChatMessagePartType = "text" + ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" +) + +type ChatMessagePart struct { + Type ChatMessagePartType `json:"type,omitempty"` + Text string `json:"text,omitempty"` + ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` +} + +type ChatCompletionMessage struct { + Role string `json:"role"` + Content string `json:"content,omitempty"` + Refusal string `json:"refusal,omitempty"` + MultiContent []ChatMessagePart + + // This property isn't in the official documentation, but it's in + // the documentation for the official library for python: + // - https://github.com/openai/openai-python/blob/main/chatml.md + // - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb + Name string `json:"name,omitempty"` + + FunctionCall *FunctionCall `json:"function_call,omitempty"` + + // For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls. + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + + // For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool. + ToolCallID string `json:"tool_call_id,omitempty"` +} + +func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) { + if m.Content != "" && m.MultiContent != nil { + return nil, ErrContentFieldsMisused + } + if len(m.MultiContent) > 0 { + msg := struct { + Role string `json:"role"` + Content string `json:"-"` + Refusal string `json:"refusal,omitempty"` + MultiContent []ChatMessagePart `json:"content,omitempty"` + Name string `json:"name,omitempty"` + FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` + }(m) + return json.Marshal(msg) + } + + msg := struct { + Role string `json:"role"` + Content string `json:"content,omitempty"` + Refusal string `json:"refusal,omitempty"` + MultiContent []ChatMessagePart `json:"-"` + Name string `json:"name,omitempty"` + FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` + }(m) + return json.Marshal(msg) +} + +func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error { + msg := struct { + Role string `json:"role"` + Content string `json:"content,omitempty"` + Refusal string `json:"refusal,omitempty"` + MultiContent []ChatMessagePart + Name string `json:"name,omitempty"` + FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` + }{} + + if err := json.Unmarshal(bs, &msg); err == nil { + *m = ChatCompletionMessage(msg) + return nil + } + multiMsg := struct { + Role string `json:"role"` + Content string + Refusal string `json:"refusal,omitempty"` + MultiContent []ChatMessagePart `json:"content"` + Name string `json:"name,omitempty"` + FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` + }{} + if err := json.Unmarshal(bs, &multiMsg); err != nil { + return err + } + *m = ChatCompletionMessage(multiMsg) + return nil +} + +type ToolCall struct { + // Index is not nil only in chat completion chunk object + Index *int `json:"index,omitempty"` + ID string `json:"id,omitempty"` + Type ToolType `json:"type"` + Function FunctionCall `json:"function"` +} + +type FunctionCall struct { + Name string `json:"name,omitempty"` + // call function with arguments in JSON format + Arguments string `json:"arguments,omitempty"` +} + +type ChatCompletionResponseFormatType string + +const ( + ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object" + ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema" + ChatCompletionResponseFormatTypeText ChatCompletionResponseFormatType = "text" +) + +type ChatCompletionResponseFormat struct { + Type ChatCompletionResponseFormatType `json:"type,omitempty"` + JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"` +} + +type ChatCompletionResponseFormatJSONSchema struct { + Name string `json:"name"` + Description string `json:"description,omitempty"` + Schema json.Marshaler `json:"schema"` + Strict bool `json:"strict"` +} + +// ChatCompletionRequest represents a request structure for chat completion API. +type ChatCompletionRequest struct { + Model string `json:"model"` + Messages []ChatCompletionMessage `json:"messages"` + // MaxTokens The maximum number of tokens that can be generated in the chat completion. + // This value can be used to control costs for text generated via API. + // This value is now deprecated in favor of max_completion_tokens, and is not compatible with o1 series models. + // refs: https://platform.openai.com/docs/api-reference/chat/create#chat-create-max_tokens + MaxTokens int `json:"max_tokens,omitempty"` + // MaxCompletionTokens An upper bound for the number of tokens that can be generated for a completion, + // including visible output tokens and reasoning tokens https://platform.openai.com/docs/guides/reasoning + MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` + Temperature float32 `json:"temperature,omitempty"` + TopP float32 `json:"top_p,omitempty"` + N int `json:"n,omitempty"` + Stream bool `json:"stream,omitempty"` + Stop []string `json:"stop,omitempty"` + PresencePenalty float32 `json:"presence_penalty,omitempty"` + ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"` + Seed *int `json:"seed,omitempty"` + FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` + // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. + // incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}` + // refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias + LogitBias map[string]int `json:"logit_bias,omitempty"` + // LogProbs indicates whether to return log probabilities of the output tokens or not. + // If true, returns the log probabilities of each output token returned in the content of message. + // This option is currently not available on the gpt-4-vision-preview model. + LogProbs bool `json:"logprobs,omitempty"` + // TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each + // token position, each with an associated log probability. + // logprobs must be set to true if this parameter is used. + TopLogProbs int `json:"top_logprobs,omitempty"` + User string `json:"user,omitempty"` + // Deprecated: use Tools instead. + Functions []FunctionDefinition `json:"functions,omitempty"` + // Deprecated: use ToolChoice instead. + FunctionCall any `json:"function_call,omitempty"` + Tools []Tool `json:"tools,omitempty"` + // This can be either a string or an ToolChoice object. + ToolChoice any `json:"tool_choice,omitempty"` + // Options for streaming response. Only set this when you set stream: true. + StreamOptions *StreamOptions `json:"stream_options,omitempty"` + // Disable the default behavior of parallel tool calls by setting it: false. + ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` + // Store can be set to true to store the output of this completion request for use in distillations and evals. + // https://platform.openai.com/docs/api-reference/chat/create#chat-create-store + Store bool `json:"store,omitempty"` + // Controls effort on reasoning for reasoning models. It can be set to "low", "medium", or "high". + ReasoningEffort string `json:"reasoning_effort,omitempty"` + // Metadata to store with the completion. + Metadata map[string]string `json:"metadata,omitempty"` +} + +type StreamOptions struct { + // If set, an additional chunk will be streamed before the data: [DONE] message. + // The usage field on this chunk shows the token usage statistics for the entire request, + // and the choices field will always be an empty array. + // All other chunks will also include a usage field, but with a null value. + IncludeUsage bool `json:"include_usage,omitempty"` +} + +type ToolType string + +const ( + ToolTypeFunction ToolType = "function" +) + +type Tool struct { + Type ToolType `json:"type"` + Function *FunctionDefinition `json:"function,omitempty"` +} + +type ToolChoice struct { + Type ToolType `json:"type"` + Function ToolFunction `json:"function,omitempty"` +} + +type ToolFunction struct { + Name string `json:"name"` +} + +type FunctionDefinition struct { + Name string `json:"name"` + Description string `json:"description,omitempty"` + Strict bool `json:"strict,omitempty"` + // Parameters is an object describing the function. + // You can pass json.RawMessage to describe the schema, + // or you can pass in a struct which serializes to the proper JSON schema. + // The jsonschema package is provided for convenience, but you should + // consider another specialized library if you require more complex schemas. + Parameters any `json:"parameters"` +} + +// Deprecated: use FunctionDefinition instead. +type FunctionDefine = FunctionDefinition + +type TopLogProbs struct { + Token string `json:"token"` + LogProb float64 `json:"logprob"` + Bytes []byte `json:"bytes,omitempty"` +} + +// LogProb represents the probability information for a token. +type LogProb struct { + Token string `json:"token"` + LogProb float64 `json:"logprob"` + Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null + // TopLogProbs is a list of the most likely tokens and their log probability, at this token position. + // In rare cases, there may be fewer than the number of requested top_logprobs returned. + TopLogProbs []TopLogProbs `json:"top_logprobs"` +} + +// LogProbs is the top-level structure containing the log probability information. +type LogProbs struct { + // Content is a list of message content tokens with log probability information. + Content []LogProb `json:"content"` +} + +type FinishReason string + +const ( + FinishReasonStop FinishReason = "stop" + FinishReasonLength FinishReason = "length" + FinishReasonFunctionCall FinishReason = "function_call" + FinishReasonToolCalls FinishReason = "tool_calls" + FinishReasonContentFilter FinishReason = "content_filter" + FinishReasonNull FinishReason = "null" +) + +func (r FinishReason) MarshalJSON() ([]byte, error) { + if r == FinishReasonNull || r == "" { + return []byte("null"), nil + } + return []byte(`"` + string(r) + `"`), nil // best effort to not break future API changes +} + +type ChatCompletionChoice struct { + Index int `json:"index"` + Message ChatCompletionMessage `json:"message"` + // FinishReason + // stop: API returned complete message, + // or a message terminated by one of the stop sequences provided via the stop parameter + // length: Incomplete model output due to max_tokens parameter or token limit + // function_call: The model decided to call a function + // content_filter: Omitted content due to a flag from our content filters + // null: API response still in progress or incomplete + FinishReason FinishReason `json:"finish_reason"` + LogProbs *LogProbs `json:"logprobs,omitempty"` + ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` +} + +// ChatCompletionResponse represents a response structure for chat completion API. +type ChatCompletionResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + Model string `json:"model"` + Choices []ChatCompletionChoice `json:"choices"` + Usage Usage `json:"usage"` + SystemFingerprint string `json:"system_fingerprint"` + PromptFilterResults []PromptFilterResult `json:"prompt_filter_results,omitempty"` + + httpHeader +} + +// CreateChatCompletion — API call to Create a completion for the chat message. +func (c *Client) CreateChatCompletion( + ctx context.Context, + request ChatCompletionRequest, +) (response ChatCompletionResponse, err error) { + if request.Stream { + err = ErrChatCompletionStreamNotSupported + return + } + + urlSuffix := chatCompletionsSuffix + if !checkEndpointSupportsModel(urlSuffix, request.Model) { + err = ErrChatCompletionInvalidModel + return + } + + reasoningValidator := NewReasoningValidator() + if err = reasoningValidator.Validate(request); err != nil { + return + } + + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/chat_stream.go b/backend/vendor/github.com/sashabaranov/go-openai/chat_stream.go new file mode 100644 index 0000000..525b445 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/chat_stream.go @@ -0,0 +1,106 @@ +package openai + +import ( + "context" + "net/http" +) + +type ChatCompletionStreamChoiceDelta struct { + Content string `json:"content,omitempty"` + Role string `json:"role,omitempty"` + FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + Refusal string `json:"refusal,omitempty"` +} + +type ChatCompletionStreamChoiceLogprobs struct { + Content []ChatCompletionTokenLogprob `json:"content,omitempty"` + Refusal []ChatCompletionTokenLogprob `json:"refusal,omitempty"` +} + +type ChatCompletionTokenLogprob struct { + Token string `json:"token"` + Bytes []int64 `json:"bytes,omitempty"` + Logprob float64 `json:"logprob,omitempty"` + TopLogprobs []ChatCompletionTokenLogprobTopLogprob `json:"top_logprobs"` +} + +type ChatCompletionTokenLogprobTopLogprob struct { + Token string `json:"token"` + Bytes []int64 `json:"bytes"` + Logprob float64 `json:"logprob"` +} + +type ChatCompletionStreamChoice struct { + Index int `json:"index"` + Delta ChatCompletionStreamChoiceDelta `json:"delta"` + Logprobs *ChatCompletionStreamChoiceLogprobs `json:"logprobs,omitempty"` + FinishReason FinishReason `json:"finish_reason"` + ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` +} + +type PromptFilterResult struct { + Index int `json:"index"` + ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` +} + +type ChatCompletionStreamResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + Model string `json:"model"` + Choices []ChatCompletionStreamChoice `json:"choices"` + SystemFingerprint string `json:"system_fingerprint"` + PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"` + PromptFilterResults []PromptFilterResult `json:"prompt_filter_results,omitempty"` + // An optional field that will only be present when you set stream_options: {"include_usage": true} in your request. + // When present, it contains a null value except for the last chunk which contains the token usage statistics + // for the entire request. + Usage *Usage `json:"usage,omitempty"` +} + +// ChatCompletionStream +// Note: Perhaps it is more elegant to abstract Stream using generics. +type ChatCompletionStream struct { + *streamReader[ChatCompletionStreamResponse] +} + +// CreateChatCompletionStream — API call to create a chat completion w/ streaming +// support. It sets whether to stream back partial progress. If set, tokens will be +// sent as data-only server-sent events as they become available, with the +// stream terminated by a data: [DONE] message. +func (c *Client) CreateChatCompletionStream( + ctx context.Context, + request ChatCompletionRequest, +) (stream *ChatCompletionStream, err error) { + urlSuffix := chatCompletionsSuffix + if !checkEndpointSupportsModel(urlSuffix, request.Model) { + err = ErrChatCompletionInvalidModel + return + } + + request.Stream = true + reasoningValidator := NewReasoningValidator() + if err = reasoningValidator.Validate(request); err != nil { + return + } + + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(request), + ) + if err != nil { + return nil, err + } + + resp, err := sendRequestStream[ChatCompletionStreamResponse](c, req) + if err != nil { + return + } + stream = &ChatCompletionStream{ + streamReader: resp, + } + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/client.go b/backend/vendor/github.com/sashabaranov/go-openai/client.go new file mode 100644 index 0000000..cef3753 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/client.go @@ -0,0 +1,327 @@ +package openai + +import ( + "bufio" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strings" + + utils "github.com/sashabaranov/go-openai/internal" +) + +// Client is OpenAI GPT-3 API client. +type Client struct { + config ClientConfig + + requestBuilder utils.RequestBuilder + createFormBuilder func(io.Writer) utils.FormBuilder +} + +type Response interface { + SetHeader(http.Header) +} + +type httpHeader http.Header + +func (h *httpHeader) SetHeader(header http.Header) { + *h = httpHeader(header) +} + +func (h *httpHeader) Header() http.Header { + return http.Header(*h) +} + +func (h *httpHeader) GetRateLimitHeaders() RateLimitHeaders { + return newRateLimitHeaders(h.Header()) +} + +type RawResponse struct { + io.ReadCloser + + httpHeader +} + +// NewClient creates new OpenAI API client. +func NewClient(authToken string) *Client { + config := DefaultConfig(authToken) + return NewClientWithConfig(config) +} + +// NewClientWithConfig creates new OpenAI API client for specified config. +func NewClientWithConfig(config ClientConfig) *Client { + return &Client{ + config: config, + requestBuilder: utils.NewRequestBuilder(), + createFormBuilder: func(body io.Writer) utils.FormBuilder { + return utils.NewFormBuilder(body) + }, + } +} + +// NewOrgClient creates new OpenAI API client for specified Organization ID. +// +// Deprecated: Please use NewClientWithConfig. +func NewOrgClient(authToken, org string) *Client { + config := DefaultConfig(authToken) + config.OrgID = org + return NewClientWithConfig(config) +} + +type requestOptions struct { + body any + header http.Header +} + +type requestOption func(*requestOptions) + +func withBody(body any) requestOption { + return func(args *requestOptions) { + args.body = body + } +} + +func withContentType(contentType string) requestOption { + return func(args *requestOptions) { + args.header.Set("Content-Type", contentType) + } +} + +func withBetaAssistantVersion(version string) requestOption { + return func(args *requestOptions) { + args.header.Set("OpenAI-Beta", fmt.Sprintf("assistants=%s", version)) + } +} + +func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) { + // Default Options + args := &requestOptions{ + body: nil, + header: make(http.Header), + } + for _, setter := range setters { + setter(args) + } + req, err := c.requestBuilder.Build(ctx, method, url, args.body, args.header) + if err != nil { + return nil, err + } + c.setCommonHeaders(req) + return req, nil +} + +func (c *Client) sendRequest(req *http.Request, v Response) error { + req.Header.Set("Accept", "application/json") + + // Check whether Content-Type is already set, Upload Files API requires + // Content-Type == multipart/form-data + contentType := req.Header.Get("Content-Type") + if contentType == "" { + req.Header.Set("Content-Type", "application/json") + } + + res, err := c.config.HTTPClient.Do(req) + if err != nil { + return err + } + + defer res.Body.Close() + + if v != nil { + v.SetHeader(res.Header) + } + + if isFailureStatusCode(res) { + return c.handleErrorResp(res) + } + + return decodeResponse(res.Body, v) +} + +func (c *Client) sendRequestRaw(req *http.Request) (response RawResponse, err error) { + resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body should be closed by outer function + if err != nil { + return + } + + if isFailureStatusCode(resp) { + err = c.handleErrorResp(resp) + return + } + + response.SetHeader(resp.Header) + response.ReadCloser = resp.Body + return +} + +func sendRequestStream[T streamable](client *Client, req *http.Request) (*streamReader[T], error) { + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "text/event-stream") + req.Header.Set("Cache-Control", "no-cache") + req.Header.Set("Connection", "keep-alive") + + resp, err := client.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close() + if err != nil { + return new(streamReader[T]), err + } + if isFailureStatusCode(resp) { + return new(streamReader[T]), client.handleErrorResp(resp) + } + return &streamReader[T]{ + emptyMessagesLimit: client.config.EmptyMessagesLimit, + reader: bufio.NewReader(resp.Body), + response: resp, + errAccumulator: utils.NewErrorAccumulator(), + unmarshaler: &utils.JSONUnmarshaler{}, + httpHeader: httpHeader(resp.Header), + }, nil +} + +func (c *Client) setCommonHeaders(req *http.Request) { + // https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication + switch c.config.APIType { + case APITypeAzure, APITypeCloudflareAzure: + // Azure API Key authentication + req.Header.Set(AzureAPIKeyHeader, c.config.authToken) + case APITypeAnthropic: + // https://docs.anthropic.com/en/api/versioning + req.Header.Set("anthropic-version", c.config.APIVersion) + case APITypeOpenAI, APITypeAzureAD: + fallthrough + default: + if c.config.authToken != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken)) + } + } + + if c.config.OrgID != "" { + req.Header.Set("OpenAI-Organization", c.config.OrgID) + } +} + +func isFailureStatusCode(resp *http.Response) bool { + return resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest +} + +func decodeResponse(body io.Reader, v any) error { + if v == nil { + return nil + } + + switch o := v.(type) { + case *string: + return decodeString(body, o) + case *audioTextResponse: + return decodeString(body, &o.Text) + default: + return json.NewDecoder(body).Decode(v) + } +} + +func decodeString(body io.Reader, output *string) error { + b, err := io.ReadAll(body) + if err != nil { + return err + } + *output = string(b) + return nil +} + +type fullURLOptions struct { + model string +} + +type fullURLOption func(*fullURLOptions) + +func withModel(model string) fullURLOption { + return func(args *fullURLOptions) { + args.model = model + } +} + +var azureDeploymentsEndpoints = []string{ + "/completions", + "/embeddings", + "/chat/completions", + "/audio/transcriptions", + "/audio/translations", + "/audio/speech", + "/images/generations", +} + +// fullURL returns full URL for request. +func (c *Client) fullURL(suffix string, setters ...fullURLOption) string { + baseURL := strings.TrimRight(c.config.BaseURL, "/") + args := fullURLOptions{} + for _, setter := range setters { + setter(&args) + } + + if c.config.APIType == APITypeAzure || c.config.APIType == APITypeAzureAD { + baseURL = c.baseURLWithAzureDeployment(baseURL, suffix, args.model) + } + + if c.config.APIVersion != "" { + suffix = c.suffixWithAPIVersion(suffix) + } + return fmt.Sprintf("%s%s", baseURL, suffix) +} + +func (c *Client) suffixWithAPIVersion(suffix string) string { + parsedSuffix, err := url.Parse(suffix) + if err != nil { + panic("failed to parse url suffix") + } + query := parsedSuffix.Query() + query.Add("api-version", c.config.APIVersion) + return fmt.Sprintf("%s?%s", parsedSuffix.Path, query.Encode()) +} + +func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newBaseURL string) { + baseURL = fmt.Sprintf("%s/%s", strings.TrimRight(baseURL, "/"), azureAPIPrefix) + if containsSubstr(azureDeploymentsEndpoints, suffix) { + azureDeploymentName := c.config.GetAzureDeploymentByModel(model) + if azureDeploymentName == "" { + azureDeploymentName = "UNKNOWN" + } + baseURL = fmt.Sprintf("%s/%s/%s", baseURL, azureDeploymentsPrefix, azureDeploymentName) + } + return baseURL +} + +func (c *Client) handleErrorResp(resp *http.Response) error { + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("error, reading response body: %w", err) + } + var errRes ErrorResponse + err = json.Unmarshal(body, &errRes) + if err != nil || errRes.Error == nil { + reqErr := &RequestError{ + HTTPStatus: resp.Status, + HTTPStatusCode: resp.StatusCode, + Err: err, + Body: body, + } + if errRes.Error != nil { + reqErr.Err = errRes.Error + } + return reqErr + } + + errRes.Error.HTTPStatus = resp.Status + errRes.Error.HTTPStatusCode = resp.StatusCode + return errRes.Error +} + +func containsSubstr(s []string, e string) bool { + for _, v := range s { + if strings.Contains(e, v) { + return true + } + } + return false +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/common.go b/backend/vendor/github.com/sashabaranov/go-openai/common.go new file mode 100644 index 0000000..8cc7289 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/common.go @@ -0,0 +1,24 @@ +package openai + +// common.go defines common types used throughout the OpenAI API. + +// Usage Represents the total token usage per request to OpenAI. +type Usage struct { + PromptTokens int `json:"prompt_tokens"` + CompletionTokens int `json:"completion_tokens"` + TotalTokens int `json:"total_tokens"` + PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details"` + CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details"` +} + +// CompletionTokensDetails Breakdown of tokens used in a completion. +type CompletionTokensDetails struct { + AudioTokens int `json:"audio_tokens"` + ReasoningTokens int `json:"reasoning_tokens"` +} + +// PromptTokensDetails Breakdown of tokens used in the prompt. +type PromptTokensDetails struct { + AudioTokens int `json:"audio_tokens"` + CachedTokens int `json:"cached_tokens"` +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/completion.go b/backend/vendor/github.com/sashabaranov/go-openai/completion.go new file mode 100644 index 0000000..015fa2a --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/completion.go @@ -0,0 +1,264 @@ +package openai + +import ( + "context" + "net/http" +) + +// GPT3 Defines the models provided by OpenAI to use when generating +// completions from OpenAI. +// GPT3 Models are designed for text-based tasks. For code-specific +// tasks, please refer to the Codex series of models. +const ( + O1Mini = "o1-mini" + O1Mini20240912 = "o1-mini-2024-09-12" + O1Preview = "o1-preview" + O1Preview20240912 = "o1-preview-2024-09-12" + O1 = "o1" + O120241217 = "o1-2024-12-17" + O3Mini = "o3-mini" + O3Mini20250131 = "o3-mini-2025-01-31" + GPT432K0613 = "gpt-4-32k-0613" + GPT432K0314 = "gpt-4-32k-0314" + GPT432K = "gpt-4-32k" + GPT40613 = "gpt-4-0613" + GPT40314 = "gpt-4-0314" + GPT4o = "gpt-4o" + GPT4o20240513 = "gpt-4o-2024-05-13" + GPT4o20240806 = "gpt-4o-2024-08-06" + GPT4o20241120 = "gpt-4o-2024-11-20" + GPT4oLatest = "chatgpt-4o-latest" + GPT4oMini = "gpt-4o-mini" + GPT4oMini20240718 = "gpt-4o-mini-2024-07-18" + GPT4Turbo = "gpt-4-turbo" + GPT4Turbo20240409 = "gpt-4-turbo-2024-04-09" + GPT4Turbo0125 = "gpt-4-0125-preview" + GPT4Turbo1106 = "gpt-4-1106-preview" + GPT4TurboPreview = "gpt-4-turbo-preview" + GPT4VisionPreview = "gpt-4-vision-preview" + GPT4 = "gpt-4" + GPT4Dot5Preview = "gpt-4.5-preview" + GPT4Dot5Preview20250227 = "gpt-4.5-preview-2025-02-27" + GPT3Dot5Turbo0125 = "gpt-3.5-turbo-0125" + GPT3Dot5Turbo1106 = "gpt-3.5-turbo-1106" + GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613" + GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301" + GPT3Dot5Turbo16K = "gpt-3.5-turbo-16k" + GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613" + GPT3Dot5Turbo = "gpt-3.5-turbo" + GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextDavinci003 = "text-davinci-003" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextDavinci002 = "text-davinci-002" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextCurie001 = "text-curie-001" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextBabbage001 = "text-babbage-001" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextAda001 = "text-ada-001" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3TextDavinci001 = "text-davinci-001" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3DavinciInstructBeta = "davinci-instruct-beta" + // Deprecated: Model is shutdown. Use davinci-002 instead. + GPT3Davinci = "davinci" + GPT3Davinci002 = "davinci-002" + // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. + GPT3CurieInstructBeta = "curie-instruct-beta" + GPT3Curie = "curie" + GPT3Curie002 = "curie-002" + // Deprecated: Model is shutdown. Use babbage-002 instead. + GPT3Ada = "ada" + GPT3Ada002 = "ada-002" + // Deprecated: Model is shutdown. Use babbage-002 instead. + GPT3Babbage = "babbage" + GPT3Babbage002 = "babbage-002" +) + +// Codex Defines the models provided by OpenAI. +// These models are designed for code-specific tasks, and use +// a different tokenizer which optimizes for whitespace. +const ( + CodexCodeDavinci002 = "code-davinci-002" + CodexCodeCushman001 = "code-cushman-001" + CodexCodeDavinci001 = "code-davinci-001" +) + +var disabledModelsForEndpoints = map[string]map[string]bool{ + "/completions": { + O1Mini: true, + O1Mini20240912: true, + O1Preview: true, + O1Preview20240912: true, + O3Mini: true, + O3Mini20250131: true, + GPT3Dot5Turbo: true, + GPT3Dot5Turbo0301: true, + GPT3Dot5Turbo0613: true, + GPT3Dot5Turbo1106: true, + GPT3Dot5Turbo0125: true, + GPT3Dot5Turbo16K: true, + GPT3Dot5Turbo16K0613: true, + GPT4: true, + GPT4Dot5Preview: true, + GPT4Dot5Preview20250227: true, + GPT4o: true, + GPT4o20240513: true, + GPT4o20240806: true, + GPT4o20241120: true, + GPT4oLatest: true, + GPT4oMini: true, + GPT4oMini20240718: true, + GPT4TurboPreview: true, + GPT4VisionPreview: true, + GPT4Turbo1106: true, + GPT4Turbo0125: true, + GPT4Turbo: true, + GPT4Turbo20240409: true, + GPT40314: true, + GPT40613: true, + GPT432K: true, + GPT432K0314: true, + GPT432K0613: true, + }, + chatCompletionsSuffix: { + CodexCodeDavinci002: true, + CodexCodeCushman001: true, + CodexCodeDavinci001: true, + GPT3TextDavinci003: true, + GPT3TextDavinci002: true, + GPT3TextCurie001: true, + GPT3TextBabbage001: true, + GPT3TextAda001: true, + GPT3TextDavinci001: true, + GPT3DavinciInstructBeta: true, + GPT3Davinci: true, + GPT3CurieInstructBeta: true, + GPT3Curie: true, + GPT3Ada: true, + GPT3Babbage: true, + }, +} + +func checkEndpointSupportsModel(endpoint, model string) bool { + return !disabledModelsForEndpoints[endpoint][model] +} + +func checkPromptType(prompt any) bool { + _, isString := prompt.(string) + _, isStringSlice := prompt.([]string) + if isString || isStringSlice { + return true + } + + // check if it is prompt is []string hidden under []any + slice, isSlice := prompt.([]any) + if !isSlice { + return false + } + + for _, item := range slice { + _, itemIsString := item.(string) + if !itemIsString { + return false + } + } + return true // all items in the slice are string, so it is []string +} + +// CompletionRequest represents a request structure for completion API. +type CompletionRequest struct { + Model string `json:"model"` + Prompt any `json:"prompt,omitempty"` + BestOf int `json:"best_of,omitempty"` + Echo bool `json:"echo,omitempty"` + FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` + // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. + // incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}` + // refs: https://platform.openai.com/docs/api-reference/completions/create#completions/create-logit_bias + LogitBias map[string]int `json:"logit_bias,omitempty"` + // Store can be set to true to store the output of this completion request for use in distillations and evals. + // https://platform.openai.com/docs/api-reference/chat/create#chat-create-store + Store bool `json:"store,omitempty"` + // Metadata to store with the completion. + Metadata map[string]string `json:"metadata,omitempty"` + LogProbs int `json:"logprobs,omitempty"` + MaxTokens int `json:"max_tokens,omitempty"` + N int `json:"n,omitempty"` + PresencePenalty float32 `json:"presence_penalty,omitempty"` + Seed *int `json:"seed,omitempty"` + Stop []string `json:"stop,omitempty"` + Stream bool `json:"stream,omitempty"` + Suffix string `json:"suffix,omitempty"` + Temperature float32 `json:"temperature,omitempty"` + TopP float32 `json:"top_p,omitempty"` + User string `json:"user,omitempty"` +} + +// CompletionChoice represents one of possible completions. +type CompletionChoice struct { + Text string `json:"text"` + Index int `json:"index"` + FinishReason string `json:"finish_reason"` + LogProbs LogprobResult `json:"logprobs"` +} + +// LogprobResult represents logprob result of Choice. +type LogprobResult struct { + Tokens []string `json:"tokens"` + TokenLogprobs []float32 `json:"token_logprobs"` + TopLogprobs []map[string]float32 `json:"top_logprobs"` + TextOffset []int `json:"text_offset"` +} + +// CompletionResponse represents a response structure for completion API. +type CompletionResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + Model string `json:"model"` + Choices []CompletionChoice `json:"choices"` + Usage Usage `json:"usage"` + + httpHeader +} + +// CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well +// as, if requested, the probabilities over each alternative token at each position. +// +// If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object, +// and the server will use the model's parameters to generate the completion. +func (c *Client) CreateCompletion( + ctx context.Context, + request CompletionRequest, +) (response CompletionResponse, err error) { + if request.Stream { + err = ErrCompletionStreamNotSupported + return + } + + urlSuffix := "/completions" + if !checkEndpointSupportsModel(urlSuffix, request.Model) { + err = ErrCompletionUnsupportedModel + return + } + + if !checkPromptType(request.Prompt) { + err = ErrCompletionRequestPromptTypeNotSupported + return + } + + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/config.go b/backend/vendor/github.com/sashabaranov/go-openai/config.go new file mode 100644 index 0000000..4788ba6 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/config.go @@ -0,0 +1,109 @@ +package openai + +import ( + "net/http" + "regexp" +) + +const ( + openaiAPIURLv1 = "https://api.openai.com/v1" + defaultEmptyMessagesLimit uint = 300 + + azureAPIPrefix = "openai" + azureDeploymentsPrefix = "deployments" + + AnthropicAPIVersion = "2023-06-01" +) + +type APIType string + +const ( + APITypeOpenAI APIType = "OPEN_AI" + APITypeAzure APIType = "AZURE" + APITypeAzureAD APIType = "AZURE_AD" + APITypeCloudflareAzure APIType = "CLOUDFLARE_AZURE" + APITypeAnthropic APIType = "ANTHROPIC" +) + +const AzureAPIKeyHeader = "api-key" + +const defaultAssistantVersion = "v2" // upgrade to v2 to support vector store + +type HTTPDoer interface { + Do(req *http.Request) (*http.Response, error) +} + +// ClientConfig is a configuration of a client. +type ClientConfig struct { + authToken string + + BaseURL string + OrgID string + APIType APIType + APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD or APITypeAnthropic + AssistantVersion string + AzureModelMapperFunc func(model string) string // replace model to azure deployment name func + HTTPClient HTTPDoer + + EmptyMessagesLimit uint +} + +func DefaultConfig(authToken string) ClientConfig { + return ClientConfig{ + authToken: authToken, + BaseURL: openaiAPIURLv1, + APIType: APITypeOpenAI, + AssistantVersion: defaultAssistantVersion, + OrgID: "", + + HTTPClient: &http.Client{}, + + EmptyMessagesLimit: defaultEmptyMessagesLimit, + } +} + +func DefaultAzureConfig(apiKey, baseURL string) ClientConfig { + return ClientConfig{ + authToken: apiKey, + BaseURL: baseURL, + OrgID: "", + APIType: APITypeAzure, + APIVersion: "2023-05-15", + AzureModelMapperFunc: func(model string) string { + return regexp.MustCompile(`[.:]`).ReplaceAllString(model, "") + }, + + HTTPClient: &http.Client{}, + + EmptyMessagesLimit: defaultEmptyMessagesLimit, + } +} + +func DefaultAnthropicConfig(apiKey, baseURL string) ClientConfig { + if baseURL == "" { + baseURL = "https://api.anthropic.com/v1" + } + return ClientConfig{ + authToken: apiKey, + BaseURL: baseURL, + OrgID: "", + APIType: APITypeAnthropic, + APIVersion: AnthropicAPIVersion, + + HTTPClient: &http.Client{}, + + EmptyMessagesLimit: defaultEmptyMessagesLimit, + } +} + +func (ClientConfig) String() string { + return "" +} + +func (c ClientConfig) GetAzureDeploymentByModel(model string) string { + if c.AzureModelMapperFunc != nil { + return c.AzureModelMapperFunc(model) + } + + return model +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/edits.go b/backend/vendor/github.com/sashabaranov/go-openai/edits.go new file mode 100644 index 0000000..fe8ecd0 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/edits.go @@ -0,0 +1,53 @@ +package openai + +import ( + "context" + "fmt" + "net/http" +) + +// EditsRequest represents a request structure for Edits API. +type EditsRequest struct { + Model *string `json:"model,omitempty"` + Input string `json:"input,omitempty"` + Instruction string `json:"instruction,omitempty"` + N int `json:"n,omitempty"` + Temperature float32 `json:"temperature,omitempty"` + TopP float32 `json:"top_p,omitempty"` +} + +// EditsChoice represents one of possible edits. +type EditsChoice struct { + Text string `json:"text"` + Index int `json:"index"` +} + +// EditsResponse represents a response structure for Edits API. +type EditsResponse struct { + Object string `json:"object"` + Created int64 `json:"created"` + Usage Usage `json:"usage"` + Choices []EditsChoice `json:"choices"` + + httpHeader +} + +// Edits Perform an API call to the Edits endpoint. +/* Deprecated: Users of the Edits API and its associated models (e.g., text-davinci-edit-001 or code-davinci-edit-001) +will need to migrate to GPT-3.5 Turbo by January 4, 2024. +You can use CreateChatCompletion or CreateChatCompletionStream instead. +*/ +func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error) { + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/edits", withModel(fmt.Sprint(request.Model))), + withBody(request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/embeddings.go b/backend/vendor/github.com/sashabaranov/go-openai/embeddings.go new file mode 100644 index 0000000..4a0e682 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/embeddings.go @@ -0,0 +1,267 @@ +package openai + +import ( + "context" + "encoding/base64" + "encoding/binary" + "errors" + "math" + "net/http" +) + +var ErrVectorLengthMismatch = errors.New("vector length mismatch") + +// EmbeddingModel enumerates the models which can be used +// to generate Embedding vectors. +type EmbeddingModel string + +const ( + // Deprecated: The following block is shut down. Use text-embedding-ada-002 instead. + AdaSimilarity EmbeddingModel = "text-similarity-ada-001" + BabbageSimilarity EmbeddingModel = "text-similarity-babbage-001" + CurieSimilarity EmbeddingModel = "text-similarity-curie-001" + DavinciSimilarity EmbeddingModel = "text-similarity-davinci-001" + AdaSearchDocument EmbeddingModel = "text-search-ada-doc-001" + AdaSearchQuery EmbeddingModel = "text-search-ada-query-001" + BabbageSearchDocument EmbeddingModel = "text-search-babbage-doc-001" + BabbageSearchQuery EmbeddingModel = "text-search-babbage-query-001" + CurieSearchDocument EmbeddingModel = "text-search-curie-doc-001" + CurieSearchQuery EmbeddingModel = "text-search-curie-query-001" + DavinciSearchDocument EmbeddingModel = "text-search-davinci-doc-001" + DavinciSearchQuery EmbeddingModel = "text-search-davinci-query-001" + AdaCodeSearchCode EmbeddingModel = "code-search-ada-code-001" + AdaCodeSearchText EmbeddingModel = "code-search-ada-text-001" + BabbageCodeSearchCode EmbeddingModel = "code-search-babbage-code-001" + BabbageCodeSearchText EmbeddingModel = "code-search-babbage-text-001" + + AdaEmbeddingV2 EmbeddingModel = "text-embedding-ada-002" + SmallEmbedding3 EmbeddingModel = "text-embedding-3-small" + LargeEmbedding3 EmbeddingModel = "text-embedding-3-large" +) + +// Embedding is a special format of data representation that can be easily utilized by machine +// learning models and algorithms. The embedding is an information dense representation of the +// semantic meaning of a piece of text. Each embedding is a vector of floating point numbers, +// such that the distance between two embeddings in the vector space is correlated with semantic similarity +// between two inputs in the original format. For example, if two texts are similar, +// then their vector representations should also be similar. +type Embedding struct { + Object string `json:"object"` + Embedding []float32 `json:"embedding"` + Index int `json:"index"` +} + +// DotProduct calculates the dot product of the embedding vector with another +// embedding vector. Both vectors must have the same length; otherwise, an +// ErrVectorLengthMismatch is returned. The method returns the calculated dot +// product as a float32 value. +func (e *Embedding) DotProduct(other *Embedding) (float32, error) { + if len(e.Embedding) != len(other.Embedding) { + return 0, ErrVectorLengthMismatch + } + + var dotProduct float32 + for i := range e.Embedding { + dotProduct += e.Embedding[i] * other.Embedding[i] + } + + return dotProduct, nil +} + +// EmbeddingResponse is the response from a Create embeddings request. +type EmbeddingResponse struct { + Object string `json:"object"` + Data []Embedding `json:"data"` + Model EmbeddingModel `json:"model"` + Usage Usage `json:"usage"` + + httpHeader +} + +type base64String string + +func (b base64String) Decode() ([]float32, error) { + decodedData, err := base64.StdEncoding.DecodeString(string(b)) + if err != nil { + return nil, err + } + + const sizeOfFloat32 = 4 + floats := make([]float32, len(decodedData)/sizeOfFloat32) + for i := 0; i < len(floats); i++ { + floats[i] = math.Float32frombits(binary.LittleEndian.Uint32(decodedData[i*4 : (i+1)*4])) + } + + return floats, nil +} + +// Base64Embedding is a container for base64 encoded embeddings. +type Base64Embedding struct { + Object string `json:"object"` + Embedding base64String `json:"embedding"` + Index int `json:"index"` +} + +// EmbeddingResponseBase64 is the response from a Create embeddings request with base64 encoding format. +type EmbeddingResponseBase64 struct { + Object string `json:"object"` + Data []Base64Embedding `json:"data"` + Model EmbeddingModel `json:"model"` + Usage Usage `json:"usage"` + + httpHeader +} + +// ToEmbeddingResponse converts an embeddingResponseBase64 to an EmbeddingResponse. +func (r *EmbeddingResponseBase64) ToEmbeddingResponse() (EmbeddingResponse, error) { + data := make([]Embedding, len(r.Data)) + + for i, base64Embedding := range r.Data { + embedding, err := base64Embedding.Embedding.Decode() + if err != nil { + return EmbeddingResponse{}, err + } + + data[i] = Embedding{ + Object: base64Embedding.Object, + Embedding: embedding, + Index: base64Embedding.Index, + } + } + + return EmbeddingResponse{ + Object: r.Object, + Model: r.Model, + Data: data, + Usage: r.Usage, + }, nil +} + +type EmbeddingRequestConverter interface { + // Needs to be of type EmbeddingRequestStrings or EmbeddingRequestTokens + Convert() EmbeddingRequest +} + +// EmbeddingEncodingFormat is the format of the embeddings data. +// Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. +// If not specified OpenAI will use "float". +type EmbeddingEncodingFormat string + +const ( + EmbeddingEncodingFormatFloat EmbeddingEncodingFormat = "float" + EmbeddingEncodingFormatBase64 EmbeddingEncodingFormat = "base64" +) + +type EmbeddingRequest struct { + Input any `json:"input"` + Model EmbeddingModel `json:"model"` + User string `json:"user,omitempty"` + EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` + // Dimensions The number of dimensions the resulting output embeddings should have. + // Only supported in text-embedding-3 and later models. + Dimensions int `json:"dimensions,omitempty"` +} + +func (r EmbeddingRequest) Convert() EmbeddingRequest { + return r +} + +// EmbeddingRequestStrings is the input to a create embeddings request with a slice of strings. +type EmbeddingRequestStrings struct { + // Input is a slice of strings for which you want to generate an Embedding vector. + // Each input must not exceed 8192 tokens in length. + // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they + // have observed inferior results when newlines are present. + // E.g. + // "The food was delicious and the waiter..." + Input []string `json:"input"` + // ID of the model to use. You can use the List models API to see all of your available models, + // or see our Model overview for descriptions of them. + Model EmbeddingModel `json:"model"` + // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. + User string `json:"user"` + // EmbeddingEncodingFormat is the format of the embeddings data. + // Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. + // If not specified OpenAI will use "float". + EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` + // Dimensions The number of dimensions the resulting output embeddings should have. + // Only supported in text-embedding-3 and later models. + Dimensions int `json:"dimensions,omitempty"` +} + +func (r EmbeddingRequestStrings) Convert() EmbeddingRequest { + return EmbeddingRequest{ + Input: r.Input, + Model: r.Model, + User: r.User, + EncodingFormat: r.EncodingFormat, + Dimensions: r.Dimensions, + } +} + +type EmbeddingRequestTokens struct { + // Input is a slice of slices of ints ([][]int) for which you want to generate an Embedding vector. + // Each input must not exceed 8192 tokens in length. + // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they + // have observed inferior results when newlines are present. + // E.g. + // "The food was delicious and the waiter..." + Input [][]int `json:"input"` + // ID of the model to use. You can use the List models API to see all of your available models, + // or see our Model overview for descriptions of them. + Model EmbeddingModel `json:"model"` + // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. + User string `json:"user"` + // EmbeddingEncodingFormat is the format of the embeddings data. + // Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. + // If not specified OpenAI will use "float". + EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` + // Dimensions The number of dimensions the resulting output embeddings should have. + // Only supported in text-embedding-3 and later models. + Dimensions int `json:"dimensions,omitempty"` +} + +func (r EmbeddingRequestTokens) Convert() EmbeddingRequest { + return EmbeddingRequest{ + Input: r.Input, + Model: r.Model, + User: r.User, + EncodingFormat: r.EncodingFormat, + Dimensions: r.Dimensions, + } +} + +// CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |body.Input|. +// https://beta.openai.com/docs/api-reference/embeddings/create +// +// Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens +// for embedding groups of text already converted to tokens. +func (c *Client) CreateEmbeddings( + ctx context.Context, + conv EmbeddingRequestConverter, +) (res EmbeddingResponse, err error) { + baseReq := conv.Convert() + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/embeddings", withModel(string(baseReq.Model))), + withBody(baseReq), + ) + if err != nil { + return + } + + if baseReq.EncodingFormat != EmbeddingEncodingFormatBase64 { + err = c.sendRequest(req, &res) + return + } + + base64Response := &EmbeddingResponseBase64{} + err = c.sendRequest(req, base64Response) + if err != nil { + return + } + + res, err = base64Response.ToEmbeddingResponse() + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/engines.go b/backend/vendor/github.com/sashabaranov/go-openai/engines.go new file mode 100644 index 0000000..5a0dba8 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/engines.go @@ -0,0 +1,52 @@ +package openai + +import ( + "context" + "fmt" + "net/http" +) + +// Engine struct represents engine from OpenAPI API. +type Engine struct { + ID string `json:"id"` + Object string `json:"object"` + Owner string `json:"owner"` + Ready bool `json:"ready"` + + httpHeader +} + +// EnginesList is a list of engines. +type EnginesList struct { + Engines []Engine `json:"data"` + + httpHeader +} + +// ListEngines Lists the currently available engines, and provides basic +// information about each option such as the owner and availability. +func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) { + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/engines")) + if err != nil { + return + } + + err = c.sendRequest(req, &engines) + return +} + +// GetEngine Retrieves an engine instance, providing basic information about +// the engine such as the owner and availability. +func (c *Client) GetEngine( + ctx context.Context, + engineID string, +) (engine Engine, err error) { + urlSuffix := fmt.Sprintf("/engines/%s", engineID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &engine) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/error.go b/backend/vendor/github.com/sashabaranov/go-openai/error.go new file mode 100644 index 0000000..8a74bd5 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/error.go @@ -0,0 +1,115 @@ +package openai + +import ( + "encoding/json" + "fmt" + "strings" +) + +// APIError provides error information returned by the OpenAI API. +// InnerError struct is only valid for Azure OpenAI Service. +type APIError struct { + Code any `json:"code,omitempty"` + Message string `json:"message"` + Param *string `json:"param,omitempty"` + Type string `json:"type"` + HTTPStatus string `json:"-"` + HTTPStatusCode int `json:"-"` + InnerError *InnerError `json:"innererror,omitempty"` +} + +// InnerError Azure Content filtering. Only valid for Azure OpenAI Service. +type InnerError struct { + Code string `json:"code,omitempty"` + ContentFilterResults ContentFilterResults `json:"content_filter_result,omitempty"` +} + +// RequestError provides information about generic request errors. +type RequestError struct { + HTTPStatus string + HTTPStatusCode int + Err error + Body []byte +} + +type ErrorResponse struct { + Error *APIError `json:"error,omitempty"` +} + +func (e *APIError) Error() string { + if e.HTTPStatusCode > 0 { + return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Message) + } + + return e.Message +} + +func (e *APIError) UnmarshalJSON(data []byte) (err error) { + var rawMap map[string]json.RawMessage + err = json.Unmarshal(data, &rawMap) + if err != nil { + return + } + + err = json.Unmarshal(rawMap["message"], &e.Message) + if err != nil { + // If the parameter field of a function call is invalid as a JSON schema + // refs: https://github.com/sashabaranov/go-openai/issues/381 + var messages []string + err = json.Unmarshal(rawMap["message"], &messages) + if err != nil { + return + } + e.Message = strings.Join(messages, ", ") + } + + // optional fields for azure openai + // refs: https://github.com/sashabaranov/go-openai/issues/343 + if _, ok := rawMap["type"]; ok { + err = json.Unmarshal(rawMap["type"], &e.Type) + if err != nil { + return + } + } + + if _, ok := rawMap["innererror"]; ok { + err = json.Unmarshal(rawMap["innererror"], &e.InnerError) + if err != nil { + return + } + } + + // optional fields + if _, ok := rawMap["param"]; ok { + err = json.Unmarshal(rawMap["param"], &e.Param) + if err != nil { + return + } + } + + if _, ok := rawMap["code"]; !ok { + return nil + } + + // if the api returned a number, we need to force an integer + // since the json package defaults to float64 + var intCode int + err = json.Unmarshal(rawMap["code"], &intCode) + if err == nil { + e.Code = intCode + return nil + } + + return json.Unmarshal(rawMap["code"], &e.Code) +} + +func (e *RequestError) Error() string { + return fmt.Sprintf( + "error, status code: %d, status: %s, message: %s, body: %s", + e.HTTPStatusCode, e.HTTPStatus, e.Err, e.Body, + ) +} + +func (e *RequestError) Unwrap() error { + return e.Err +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/files.go b/backend/vendor/github.com/sashabaranov/go-openai/files.go new file mode 100644 index 0000000..edc9f2a --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/files.go @@ -0,0 +1,171 @@ +package openai + +import ( + "bytes" + "context" + "fmt" + "net/http" + "os" +) + +type FileRequest struct { + FileName string `json:"file"` + FilePath string `json:"-"` + Purpose string `json:"purpose"` +} + +// PurposeType represents the purpose of the file when uploading. +type PurposeType string + +const ( + PurposeFineTune PurposeType = "fine-tune" + PurposeFineTuneResults PurposeType = "fine-tune-results" + PurposeAssistants PurposeType = "assistants" + PurposeAssistantsOutput PurposeType = "assistants_output" + PurposeBatch PurposeType = "batch" +) + +// FileBytesRequest represents a file upload request. +type FileBytesRequest struct { + // the name of the uploaded file in OpenAI + Name string + // the bytes of the file + Bytes []byte + // the purpose of the file + Purpose PurposeType +} + +// File struct represents an OpenAPI file. +type File struct { + Bytes int `json:"bytes"` + CreatedAt int64 `json:"created_at"` + ID string `json:"id"` + FileName string `json:"filename"` + Object string `json:"object"` + Status string `json:"status"` + Purpose string `json:"purpose"` + StatusDetails string `json:"status_details"` + + httpHeader +} + +// FilesList is a list of files that belong to the user or organization. +type FilesList struct { + Files []File `json:"data"` + + httpHeader +} + +// CreateFileBytes uploads bytes directly to OpenAI without requiring a local file. +func (c *Client) CreateFileBytes(ctx context.Context, request FileBytesRequest) (file File, err error) { + var b bytes.Buffer + reader := bytes.NewReader(request.Bytes) + builder := c.createFormBuilder(&b) + + err = builder.WriteField("purpose", string(request.Purpose)) + if err != nil { + return + } + + err = builder.CreateFormFileReader("file", reader, request.Name) + if err != nil { + return + } + + err = builder.Close() + if err != nil { + return + } + + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"), + withBody(&b), withContentType(builder.FormDataContentType())) + if err != nil { + return + } + + err = c.sendRequest(req, &file) + return +} + +// CreateFile uploads a jsonl file to GPT3 +// FilePath must be a local file path. +func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error) { + var b bytes.Buffer + builder := c.createFormBuilder(&b) + + err = builder.WriteField("purpose", request.Purpose) + if err != nil { + return + } + + fileData, err := os.Open(request.FilePath) + if err != nil { + return + } + defer fileData.Close() + + err = builder.CreateFormFile("file", fileData) + if err != nil { + return + } + + err = builder.Close() + if err != nil { + return + } + + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"), + withBody(&b), withContentType(builder.FormDataContentType())) + if err != nil { + return + } + + err = c.sendRequest(req, &file) + return +} + +// DeleteFile deletes an existing file. +func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) { + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/files/"+fileID)) + if err != nil { + return + } + + err = c.sendRequest(req, nil) + return +} + +// ListFiles Lists the currently available files, +// and provides basic information about each file such as the file name and purpose. +func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) { + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/files")) + if err != nil { + return + } + + err = c.sendRequest(req, &files) + return +} + +// GetFile Retrieves a file instance, providing basic information about the file +// such as the file name and purpose. +func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) { + urlSuffix := fmt.Sprintf("/files/%s", fileID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &file) + return +} + +func (c *Client) GetFileContent(ctx context.Context, fileID string) (content RawResponse, err error) { + urlSuffix := fmt.Sprintf("/files/%s/content", fileID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + return c.sendRequestRaw(req) +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/fine_tunes.go b/backend/vendor/github.com/sashabaranov/go-openai/fine_tunes.go new file mode 100644 index 0000000..74b47bf --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/fine_tunes.go @@ -0,0 +1,178 @@ +package openai + +import ( + "context" + "fmt" + "net/http" +) + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneRequest struct { + TrainingFile string `json:"training_file"` + ValidationFile string `json:"validation_file,omitempty"` + Model string `json:"model,omitempty"` + Epochs int `json:"n_epochs,omitempty"` + BatchSize int `json:"batch_size,omitempty"` + LearningRateMultiplier float32 `json:"learning_rate_multiplier,omitempty"` + PromptLossRate float32 `json:"prompt_loss_rate,omitempty"` + ComputeClassificationMetrics bool `json:"compute_classification_metrics,omitempty"` + ClassificationClasses int `json:"classification_n_classes,omitempty"` + ClassificationPositiveClass string `json:"classification_positive_class,omitempty"` + ClassificationBetas []float32 `json:"classification_betas,omitempty"` + Suffix string `json:"suffix,omitempty"` +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTune struct { + ID string `json:"id"` + Object string `json:"object"` + Model string `json:"model"` + CreatedAt int64 `json:"created_at"` + FineTuneEventList []FineTuneEvent `json:"events,omitempty"` + FineTunedModel string `json:"fine_tuned_model"` + HyperParams FineTuneHyperParams `json:"hyperparams"` + OrganizationID string `json:"organization_id"` + ResultFiles []File `json:"result_files"` + Status string `json:"status"` + ValidationFiles []File `json:"validation_files"` + TrainingFiles []File `json:"training_files"` + UpdatedAt int64 `json:"updated_at"` + + httpHeader +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneEvent struct { + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Level string `json:"level"` + Message string `json:"message"` +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneHyperParams struct { + BatchSize int `json:"batch_size"` + LearningRateMultiplier float64 `json:"learning_rate_multiplier"` + Epochs int `json:"n_epochs"` + PromptLossWeight float64 `json:"prompt_loss_weight"` +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneList struct { + Object string `json:"object"` + Data []FineTune `json:"data"` + + httpHeader +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneEventList struct { + Object string `json:"object"` + Data []FineTuneEvent `json:"data"` + + httpHeader +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +type FineTuneDeleteResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (response FineTune, err error) { + urlSuffix := "/fine-tunes" + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// CancelFineTune cancel a fine-tune job. +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) { + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel")) //nolint:lll //this method is deprecated + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error) { + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/fine-tunes")) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) { + urlSuffix := fmt.Sprintf("/fine-tunes/%s", fineTuneID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (response FineTuneDeleteResponse, err error) { + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/fine-tunes/"+fineTuneID)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. +// This API will be officially deprecated on January 4th, 2024. +// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. +func (c *Client) ListFineTuneEvents(ctx context.Context, fineTuneID string) (response FineTuneEventList, err error) { + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/fine-tunes/"+fineTuneID+"/events")) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/fine_tuning_job.go b/backend/vendor/github.com/sashabaranov/go-openai/fine_tuning_job.go new file mode 100644 index 0000000..5a9f54a --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/fine_tuning_job.go @@ -0,0 +1,159 @@ +package openai + +import ( + "context" + "fmt" + "net/http" + "net/url" +) + +type FineTuningJob struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + FinishedAt int64 `json:"finished_at"` + Model string `json:"model"` + FineTunedModel string `json:"fine_tuned_model,omitempty"` + OrganizationID string `json:"organization_id"` + Status string `json:"status"` + Hyperparameters Hyperparameters `json:"hyperparameters"` + TrainingFile string `json:"training_file"` + ValidationFile string `json:"validation_file,omitempty"` + ResultFiles []string `json:"result_files"` + TrainedTokens int `json:"trained_tokens"` + + httpHeader +} + +type Hyperparameters struct { + Epochs any `json:"n_epochs,omitempty"` + LearningRateMultiplier any `json:"learning_rate_multiplier,omitempty"` + BatchSize any `json:"batch_size,omitempty"` +} + +type FineTuningJobRequest struct { + TrainingFile string `json:"training_file"` + ValidationFile string `json:"validation_file,omitempty"` + Model string `json:"model,omitempty"` + Hyperparameters *Hyperparameters `json:"hyperparameters,omitempty"` + Suffix string `json:"suffix,omitempty"` +} + +type FineTuningJobEventList struct { + Object string `json:"object"` + Data []FineTuneEvent `json:"data"` + HasMore bool `json:"has_more"` + + httpHeader +} + +type FineTuningJobEvent struct { + Object string `json:"object"` + ID string `json:"id"` + CreatedAt int `json:"created_at"` + Level string `json:"level"` + Message string `json:"message"` + Data any `json:"data"` + Type string `json:"type"` +} + +// CreateFineTuningJob create a fine tuning job. +func (c *Client) CreateFineTuningJob( + ctx context.Context, + request FineTuningJobRequest, +) (response FineTuningJob, err error) { + urlSuffix := "/fine_tuning/jobs" + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// CancelFineTuningJob cancel a fine tuning job. +func (c *Client) CancelFineTuningJob(ctx context.Context, fineTuningJobID string) (response FineTuningJob, err error) { + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/cancel")) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveFineTuningJob retrieve a fine tuning job. +func (c *Client) RetrieveFineTuningJob( + ctx context.Context, + fineTuningJobID string, +) (response FineTuningJob, err error) { + urlSuffix := fmt.Sprintf("/fine_tuning/jobs/%s", fineTuningJobID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +type listFineTuningJobEventsParameters struct { + after *string + limit *int +} + +type ListFineTuningJobEventsParameter func(*listFineTuningJobEventsParameters) + +func ListFineTuningJobEventsWithAfter(after string) ListFineTuningJobEventsParameter { + return func(args *listFineTuningJobEventsParameters) { + args.after = &after + } +} + +func ListFineTuningJobEventsWithLimit(limit int) ListFineTuningJobEventsParameter { + return func(args *listFineTuningJobEventsParameters) { + args.limit = &limit + } +} + +// ListFineTuningJobs list fine tuning jobs events. +func (c *Client) ListFineTuningJobEvents( + ctx context.Context, + fineTuningJobID string, + setters ...ListFineTuningJobEventsParameter, +) (response FineTuningJobEventList, err error) { + parameters := &listFineTuningJobEventsParameters{ + after: nil, + limit: nil, + } + + for _, setter := range setters { + setter(parameters) + } + + urlValues := url.Values{} + if parameters.after != nil { + urlValues.Add("after", *parameters.after) + } + if parameters.limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *parameters.limit)) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/events"+encodedValues), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/image.go b/backend/vendor/github.com/sashabaranov/go-openai/image.go new file mode 100644 index 0000000..577d7db --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/image.go @@ -0,0 +1,209 @@ +package openai + +import ( + "bytes" + "context" + "net/http" + "os" + "strconv" +) + +// Image sizes defined by the OpenAI API. +const ( + CreateImageSize256x256 = "256x256" + CreateImageSize512x512 = "512x512" + CreateImageSize1024x1024 = "1024x1024" + // dall-e-3 supported only. + CreateImageSize1792x1024 = "1792x1024" + CreateImageSize1024x1792 = "1024x1792" +) + +const ( + CreateImageResponseFormatURL = "url" + CreateImageResponseFormatB64JSON = "b64_json" +) + +const ( + CreateImageModelDallE2 = "dall-e-2" + CreateImageModelDallE3 = "dall-e-3" +) + +const ( + CreateImageQualityHD = "hd" + CreateImageQualityStandard = "standard" +) + +const ( + CreateImageStyleVivid = "vivid" + CreateImageStyleNatural = "natural" +) + +// ImageRequest represents the request structure for the image API. +type ImageRequest struct { + Prompt string `json:"prompt,omitempty"` + Model string `json:"model,omitempty"` + N int `json:"n,omitempty"` + Quality string `json:"quality,omitempty"` + Size string `json:"size,omitempty"` + Style string `json:"style,omitempty"` + ResponseFormat string `json:"response_format,omitempty"` + User string `json:"user,omitempty"` +} + +// ImageResponse represents a response structure for image API. +type ImageResponse struct { + Created int64 `json:"created,omitempty"` + Data []ImageResponseDataInner `json:"data,omitempty"` + + httpHeader +} + +// ImageResponseDataInner represents a response data structure for image API. +type ImageResponseDataInner struct { + URL string `json:"url,omitempty"` + B64JSON string `json:"b64_json,omitempty"` + RevisedPrompt string `json:"revised_prompt,omitempty"` +} + +// CreateImage - API call to create an image. This is the main endpoint of the DALL-E API. +func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error) { + urlSuffix := "/images/generations" + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ImageEditRequest represents the request structure for the image API. +type ImageEditRequest struct { + Image *os.File `json:"image,omitempty"` + Mask *os.File `json:"mask,omitempty"` + Prompt string `json:"prompt,omitempty"` + Model string `json:"model,omitempty"` + N int `json:"n,omitempty"` + Size string `json:"size,omitempty"` + ResponseFormat string `json:"response_format,omitempty"` +} + +// CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API. +func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error) { + body := &bytes.Buffer{} + builder := c.createFormBuilder(body) + + // image + err = builder.CreateFormFile("image", request.Image) + if err != nil { + return + } + + // mask, it is optional + if request.Mask != nil { + err = builder.CreateFormFile("mask", request.Mask) + if err != nil { + return + } + } + + err = builder.WriteField("prompt", request.Prompt) + if err != nil { + return + } + + err = builder.WriteField("n", strconv.Itoa(request.N)) + if err != nil { + return + } + + err = builder.WriteField("size", request.Size) + if err != nil { + return + } + + err = builder.WriteField("response_format", request.ResponseFormat) + if err != nil { + return + } + + err = builder.Close() + if err != nil { + return + } + + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/images/edits", withModel(request.Model)), + withBody(body), + withContentType(builder.FormDataContentType()), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ImageVariRequest represents the request structure for the image API. +type ImageVariRequest struct { + Image *os.File `json:"image,omitempty"` + Model string `json:"model,omitempty"` + N int `json:"n,omitempty"` + Size string `json:"size,omitempty"` + ResponseFormat string `json:"response_format,omitempty"` +} + +// CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API. +// Use abbreviations(vari for variation) because ci-lint has a single-line length limit ... +func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error) { + body := &bytes.Buffer{} + builder := c.createFormBuilder(body) + + // image + err = builder.CreateFormFile("image", request.Image) + if err != nil { + return + } + + err = builder.WriteField("n", strconv.Itoa(request.N)) + if err != nil { + return + } + + err = builder.WriteField("size", request.Size) + if err != nil { + return + } + + err = builder.WriteField("response_format", request.ResponseFormat) + if err != nil { + return + } + + err = builder.Close() + if err != nil { + return + } + + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/images/variations", withModel(request.Model)), + withBody(body), + withContentType(builder.FormDataContentType()), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/internal/error_accumulator.go b/backend/vendor/github.com/sashabaranov/go-openai/internal/error_accumulator.go new file mode 100644 index 0000000..3d3e805 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/internal/error_accumulator.go @@ -0,0 +1,44 @@ +package openai + +import ( + "bytes" + "fmt" + "io" +) + +type ErrorAccumulator interface { + Write(p []byte) error + Bytes() []byte +} + +type errorBuffer interface { + io.Writer + Len() int + Bytes() []byte +} + +type DefaultErrorAccumulator struct { + Buffer errorBuffer +} + +func NewErrorAccumulator() ErrorAccumulator { + return &DefaultErrorAccumulator{ + Buffer: &bytes.Buffer{}, + } +} + +func (e *DefaultErrorAccumulator) Write(p []byte) error { + _, err := e.Buffer.Write(p) + if err != nil { + return fmt.Errorf("error accumulator write error, %w", err) + } + return nil +} + +func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) { + if e.Buffer.Len() == 0 { + return + } + errBytes = e.Buffer.Bytes() + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/internal/form_builder.go b/backend/vendor/github.com/sashabaranov/go-openai/internal/form_builder.go new file mode 100644 index 0000000..2224fad --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/internal/form_builder.go @@ -0,0 +1,65 @@ +package openai + +import ( + "fmt" + "io" + "mime/multipart" + "os" + "path" +) + +type FormBuilder interface { + CreateFormFile(fieldname string, file *os.File) error + CreateFormFileReader(fieldname string, r io.Reader, filename string) error + WriteField(fieldname, value string) error + Close() error + FormDataContentType() string +} + +type DefaultFormBuilder struct { + writer *multipart.Writer +} + +func NewFormBuilder(body io.Writer) *DefaultFormBuilder { + return &DefaultFormBuilder{ + writer: multipart.NewWriter(body), + } +} + +func (fb *DefaultFormBuilder) CreateFormFile(fieldname string, file *os.File) error { + return fb.createFormFile(fieldname, file, file.Name()) +} + +func (fb *DefaultFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error { + return fb.createFormFile(fieldname, r, path.Base(filename)) +} + +func (fb *DefaultFormBuilder) createFormFile(fieldname string, r io.Reader, filename string) error { + if filename == "" { + return fmt.Errorf("filename cannot be empty") + } + + fieldWriter, err := fb.writer.CreateFormFile(fieldname, filename) + if err != nil { + return err + } + + _, err = io.Copy(fieldWriter, r) + if err != nil { + return err + } + + return nil +} + +func (fb *DefaultFormBuilder) WriteField(fieldname, value string) error { + return fb.writer.WriteField(fieldname, value) +} + +func (fb *DefaultFormBuilder) Close() error { + return fb.writer.Close() +} + +func (fb *DefaultFormBuilder) FormDataContentType() string { + return fb.writer.FormDataContentType() +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/internal/marshaller.go b/backend/vendor/github.com/sashabaranov/go-openai/internal/marshaller.go new file mode 100644 index 0000000..223a4dc --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/internal/marshaller.go @@ -0,0 +1,15 @@ +package openai + +import ( + "encoding/json" +) + +type Marshaller interface { + Marshal(value any) ([]byte, error) +} + +type JSONMarshaller struct{} + +func (jm *JSONMarshaller) Marshal(value any) ([]byte, error) { + return json.Marshal(value) +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/internal/request_builder.go b/backend/vendor/github.com/sashabaranov/go-openai/internal/request_builder.go new file mode 100644 index 0000000..5699f6b --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/internal/request_builder.go @@ -0,0 +1,52 @@ +package openai + +import ( + "bytes" + "context" + "io" + "net/http" +) + +type RequestBuilder interface { + Build(ctx context.Context, method, url string, body any, header http.Header) (*http.Request, error) +} + +type HTTPRequestBuilder struct { + marshaller Marshaller +} + +func NewRequestBuilder() *HTTPRequestBuilder { + return &HTTPRequestBuilder{ + marshaller: &JSONMarshaller{}, + } +} + +func (b *HTTPRequestBuilder) Build( + ctx context.Context, + method string, + url string, + body any, + header http.Header, +) (req *http.Request, err error) { + var bodyReader io.Reader + if body != nil { + if v, ok := body.(io.Reader); ok { + bodyReader = v + } else { + var reqBytes []byte + reqBytes, err = b.marshaller.Marshal(body) + if err != nil { + return + } + bodyReader = bytes.NewBuffer(reqBytes) + } + } + req, err = http.NewRequestWithContext(ctx, method, url, bodyReader) + if err != nil { + return + } + if header != nil { + req.Header = header + } + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/internal/unmarshaler.go b/backend/vendor/github.com/sashabaranov/go-openai/internal/unmarshaler.go new file mode 100644 index 0000000..8828760 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/internal/unmarshaler.go @@ -0,0 +1,15 @@ +package openai + +import ( + "encoding/json" +) + +type Unmarshaler interface { + Unmarshal(data []byte, v any) error +} + +type JSONUnmarshaler struct{} + +func (jm *JSONUnmarshaler) Unmarshal(data []byte, v any) error { + return json.Unmarshal(data, v) +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/messages.go b/backend/vendor/github.com/sashabaranov/go-openai/messages.go new file mode 100644 index 0000000..3852d2e --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/messages.go @@ -0,0 +1,224 @@ +package openai + +import ( + "context" + "fmt" + "net/http" + "net/url" +) + +const ( + messagesSuffix = "messages" +) + +type Message struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int `json:"created_at"` + ThreadID string `json:"thread_id"` + Role string `json:"role"` + Content []MessageContent `json:"content"` + FileIds []string `json:"file_ids"` //nolint:revive //backwards-compatibility + AssistantID *string `json:"assistant_id,omitempty"` + RunID *string `json:"run_id,omitempty"` + Metadata map[string]any `json:"metadata"` + + httpHeader +} + +type MessagesList struct { + Messages []Message `json:"data"` + + Object string `json:"object"` + FirstID *string `json:"first_id"` + LastID *string `json:"last_id"` + HasMore bool `json:"has_more"` + + httpHeader +} + +type MessageContent struct { + Type string `json:"type"` + Text *MessageText `json:"text,omitempty"` + ImageFile *ImageFile `json:"image_file,omitempty"` + ImageURL *ImageURL `json:"image_url,omitempty"` +} +type MessageText struct { + Value string `json:"value"` + Annotations []any `json:"annotations"` +} + +type ImageFile struct { + FileID string `json:"file_id"` +} + +type ImageURL struct { + URL string `json:"url"` + Detail string `json:"detail"` +} + +type MessageRequest struct { + Role string `json:"role"` + Content string `json:"content"` + FileIds []string `json:"file_ids,omitempty"` //nolint:revive // backwards-compatibility + Metadata map[string]any `json:"metadata,omitempty"` + Attachments []ThreadAttachment `json:"attachments,omitempty"` +} + +type MessageFile struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int `json:"created_at"` + MessageID string `json:"message_id"` + + httpHeader +} + +type MessageFilesList struct { + MessageFiles []MessageFile `json:"data"` + + httpHeader +} + +type MessageDeletionStatus struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +// CreateMessage creates a new message. +func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &msg) + return +} + +// ListMessage fetches all messages in the thread. +func (c *Client) ListMessage(ctx context.Context, threadID string, + limit *int, + order *string, + after *string, + before *string, + runID *string, +) (messages MessagesList, err error) { + urlValues := url.Values{} + if limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *limit)) + } + if order != nil { + urlValues.Add("order", *order) + } + if after != nil { + urlValues.Add("after", *after) + } + if before != nil { + urlValues.Add("before", *before) + } + if runID != nil { + urlValues.Add("run_id", *runID) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("/threads/%s/%s%s", threadID, messagesSuffix, encodedValues) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &messages) + return +} + +// RetrieveMessage retrieves a Message. +func (c *Client) RetrieveMessage( + ctx context.Context, + threadID, messageID string, +) (msg Message, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &msg) + return +} + +// ModifyMessage modifies a message. +func (c *Client) ModifyMessage( + ctx context.Context, + threadID, messageID string, + metadata map[string]string, +) (msg Message, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), + withBody(map[string]any{"metadata": metadata}), withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &msg) + return +} + +// RetrieveMessageFile fetches a message file. +func (c *Client) RetrieveMessageFile( + ctx context.Context, + threadID, messageID, fileID string, +) (file MessageFile, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files/%s", threadID, messagesSuffix, messageID, fileID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &file) + return +} + +// ListMessageFiles fetches all files attached to a message. +func (c *Client) ListMessageFiles( + ctx context.Context, + threadID, messageID string, +) (files MessageFilesList, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files", threadID, messagesSuffix, messageID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &files) + return +} + +// DeleteMessage deletes a message.. +func (c *Client) DeleteMessage( + ctx context.Context, + threadID, messageID string, +) (status MessageDeletionStatus, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &status) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/models.go b/backend/vendor/github.com/sashabaranov/go-openai/models.go new file mode 100644 index 0000000..d94f988 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/models.go @@ -0,0 +1,90 @@ +package openai + +import ( + "context" + "fmt" + "net/http" +) + +// Model struct represents an OpenAPI model. +type Model struct { + CreatedAt int64 `json:"created"` + ID string `json:"id"` + Object string `json:"object"` + OwnedBy string `json:"owned_by"` + Permission []Permission `json:"permission"` + Root string `json:"root"` + Parent string `json:"parent"` + + httpHeader +} + +// Permission struct represents an OpenAPI permission. +type Permission struct { + CreatedAt int64 `json:"created"` + ID string `json:"id"` + Object string `json:"object"` + AllowCreateEngine bool `json:"allow_create_engine"` + AllowSampling bool `json:"allow_sampling"` + AllowLogprobs bool `json:"allow_logprobs"` + AllowSearchIndices bool `json:"allow_search_indices"` + AllowView bool `json:"allow_view"` + AllowFineTuning bool `json:"allow_fine_tuning"` + Organization string `json:"organization"` + Group interface{} `json:"group"` + IsBlocking bool `json:"is_blocking"` +} + +// FineTuneModelDeleteResponse represents the deletion status of a fine-tuned model. +type FineTuneModelDeleteResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +// ModelsList is a list of models, including those that belong to the user or organization. +type ModelsList struct { + Models []Model `json:"data"` + + httpHeader +} + +// ListModels Lists the currently available models, +// and provides basic information about each model such as the model id and parent. +func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error) { + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/models")) + if err != nil { + return + } + + err = c.sendRequest(req, &models) + return +} + +// GetModel Retrieves a model instance, providing basic information about +// the model such as the owner and permissioning. +func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err error) { + urlSuffix := fmt.Sprintf("/models/%s", modelID) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) + if err != nil { + return + } + + err = c.sendRequest(req, &model) + return +} + +// DeleteFineTuneModel Deletes a fine-tune model. You must have the Owner +// role in your organization to delete a model. +func (c *Client) DeleteFineTuneModel(ctx context.Context, modelID string) ( + response FineTuneModelDeleteResponse, err error) { + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/models/"+modelID)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/moderation.go b/backend/vendor/github.com/sashabaranov/go-openai/moderation.go new file mode 100644 index 0000000..a0e09c0 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/moderation.go @@ -0,0 +1,107 @@ +package openai + +import ( + "context" + "errors" + "net/http" +) + +// The moderation endpoint is a tool you can use to check whether content complies with OpenAI's usage policies. +// Developers can thus identify content that our usage policies prohibits and take action, for instance by filtering it. + +// The default is text-moderation-latest which will be automatically upgraded over time. +// This ensures you are always using our most accurate model. +// If you use text-moderation-stable, we will provide advanced notice before updating the model. +// Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest. +const ( + ModerationOmniLatest = "omni-moderation-latest" + ModerationOmni20240926 = "omni-moderation-2024-09-26" + ModerationTextStable = "text-moderation-stable" + ModerationTextLatest = "text-moderation-latest" + // Deprecated: use ModerationTextStable and ModerationTextLatest instead. + ModerationText001 = "text-moderation-001" +) + +var ( + ErrModerationInvalidModel = errors.New("this model is not supported with moderation, please use text-moderation-stable or text-moderation-latest instead") //nolint:lll +) + +var validModerationModel = map[string]struct{}{ + ModerationOmniLatest: {}, + ModerationOmni20240926: {}, + ModerationTextStable: {}, + ModerationTextLatest: {}, +} + +// ModerationRequest represents a request structure for moderation API. +type ModerationRequest struct { + Input string `json:"input,omitempty"` + Model string `json:"model,omitempty"` +} + +// Result represents one of possible moderation results. +type Result struct { + Categories ResultCategories `json:"categories"` + CategoryScores ResultCategoryScores `json:"category_scores"` + Flagged bool `json:"flagged"` +} + +// ResultCategories represents Categories of Result. +type ResultCategories struct { + Hate bool `json:"hate"` + HateThreatening bool `json:"hate/threatening"` + Harassment bool `json:"harassment"` + HarassmentThreatening bool `json:"harassment/threatening"` + SelfHarm bool `json:"self-harm"` + SelfHarmIntent bool `json:"self-harm/intent"` + SelfHarmInstructions bool `json:"self-harm/instructions"` + Sexual bool `json:"sexual"` + SexualMinors bool `json:"sexual/minors"` + Violence bool `json:"violence"` + ViolenceGraphic bool `json:"violence/graphic"` +} + +// ResultCategoryScores represents CategoryScores of Result. +type ResultCategoryScores struct { + Hate float32 `json:"hate"` + HateThreatening float32 `json:"hate/threatening"` + Harassment float32 `json:"harassment"` + HarassmentThreatening float32 `json:"harassment/threatening"` + SelfHarm float32 `json:"self-harm"` + SelfHarmIntent float32 `json:"self-harm/intent"` + SelfHarmInstructions float32 `json:"self-harm/instructions"` + Sexual float32 `json:"sexual"` + SexualMinors float32 `json:"sexual/minors"` + Violence float32 `json:"violence"` + ViolenceGraphic float32 `json:"violence/graphic"` +} + +// ModerationResponse represents a response structure for moderation API. +type ModerationResponse struct { + ID string `json:"id"` + Model string `json:"model"` + Results []Result `json:"results"` + + httpHeader +} + +// Moderations — perform a moderation api call over a string. +// Input can be an array or slice but a string will reduce the complexity. +func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error) { + if _, ok := validModerationModel[request.Model]; len(request.Model) > 0 && !ok { + err = ErrModerationInvalidModel + return + } + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/moderations", withModel(request.Model)), + withBody(&request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/ratelimit.go b/backend/vendor/github.com/sashabaranov/go-openai/ratelimit.go new file mode 100644 index 0000000..e8953f7 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/ratelimit.go @@ -0,0 +1,43 @@ +package openai + +import ( + "net/http" + "strconv" + "time" +) + +// RateLimitHeaders struct represents Openai rate limits headers. +type RateLimitHeaders struct { + LimitRequests int `json:"x-ratelimit-limit-requests"` + LimitTokens int `json:"x-ratelimit-limit-tokens"` + RemainingRequests int `json:"x-ratelimit-remaining-requests"` + RemainingTokens int `json:"x-ratelimit-remaining-tokens"` + ResetRequests ResetTime `json:"x-ratelimit-reset-requests"` + ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"` +} + +type ResetTime string + +func (r ResetTime) String() string { + return string(r) +} + +func (r ResetTime) Time() time.Time { + d, _ := time.ParseDuration(string(r)) + return time.Now().Add(d) +} + +func newRateLimitHeaders(h http.Header) RateLimitHeaders { + limitReq, _ := strconv.Atoi(h.Get("x-ratelimit-limit-requests")) + limitTokens, _ := strconv.Atoi(h.Get("x-ratelimit-limit-tokens")) + remainingReq, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-requests")) + remainingTokens, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-tokens")) + return RateLimitHeaders{ + LimitRequests: limitReq, + LimitTokens: limitTokens, + RemainingRequests: remainingReq, + RemainingTokens: remainingTokens, + ResetRequests: ResetTime(h.Get("x-ratelimit-reset-requests")), + ResetTokens: ResetTime(h.Get("x-ratelimit-reset-tokens")), + } +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/reasoning_validator.go b/backend/vendor/github.com/sashabaranov/go-openai/reasoning_validator.go new file mode 100644 index 0000000..040d6b4 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/reasoning_validator.go @@ -0,0 +1,80 @@ +package openai + +import ( + "errors" + "strings" +) + +var ( + // Deprecated: use ErrReasoningModelMaxTokensDeprecated instead. + ErrO1MaxTokensDeprecated = errors.New("this model is not supported MaxTokens, please use MaxCompletionTokens") //nolint:lll + ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll + ErrCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateCompletionStream") //nolint:lll + ErrCompletionRequestPromptTypeNotSupported = errors.New("the type of CompletionRequest.Prompt only supports string and []string") //nolint:lll +) + +var ( + ErrO1BetaLimitationsMessageTypes = errors.New("this model has beta-limitations, user and assistant messages only, system messages are not supported") //nolint:lll + ErrO1BetaLimitationsTools = errors.New("this model has beta-limitations, tools, function calling, and response format parameters are not supported") //nolint:lll + // Deprecated: use ErrReasoningModelLimitations* instead. + ErrO1BetaLimitationsLogprobs = errors.New("this model has beta-limitations, logprobs not supported") //nolint:lll + ErrO1BetaLimitationsOther = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll +) + +var ( + //nolint:lll + ErrReasoningModelMaxTokensDeprecated = errors.New("this model is not supported MaxTokens, please use MaxCompletionTokens") + ErrReasoningModelLimitationsLogprobs = errors.New("this model has beta-limitations, logprobs not supported") //nolint:lll + ErrReasoningModelLimitationsOther = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll +) + +// ReasoningValidator handles validation for o-series model requests. +type ReasoningValidator struct{} + +// NewReasoningValidator creates a new validator for o-series models. +func NewReasoningValidator() *ReasoningValidator { + return &ReasoningValidator{} +} + +// Validate performs all validation checks for o-series models. +func (v *ReasoningValidator) Validate(request ChatCompletionRequest) error { + o1Series := strings.HasPrefix(request.Model, "o1") + o3Series := strings.HasPrefix(request.Model, "o3") + + if !o1Series && !o3Series { + return nil + } + + if err := v.validateReasoningModelParams(request); err != nil { + return err + } + + return nil +} + +// validateReasoningModelParams checks reasoning model parameters. +func (v *ReasoningValidator) validateReasoningModelParams(request ChatCompletionRequest) error { + if request.MaxTokens > 0 { + return ErrReasoningModelMaxTokensDeprecated + } + if request.LogProbs { + return ErrReasoningModelLimitationsLogprobs + } + if request.Temperature > 0 && request.Temperature != 1 { + return ErrReasoningModelLimitationsOther + } + if request.TopP > 0 && request.TopP != 1 { + return ErrReasoningModelLimitationsOther + } + if request.N > 0 && request.N != 1 { + return ErrReasoningModelLimitationsOther + } + if request.PresencePenalty > 0 { + return ErrReasoningModelLimitationsOther + } + if request.FrequencyPenalty > 0 { + return ErrReasoningModelLimitationsOther + } + + return nil +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/run.go b/backend/vendor/github.com/sashabaranov/go-openai/run.go new file mode 100644 index 0000000..9c51aaf --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/run.go @@ -0,0 +1,454 @@ +package openai + +import ( + "context" + "fmt" + "net/http" + "net/url" +) + +type Run struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + ThreadID string `json:"thread_id"` + AssistantID string `json:"assistant_id"` + Status RunStatus `json:"status"` + RequiredAction *RunRequiredAction `json:"required_action,omitempty"` + LastError *RunLastError `json:"last_error,omitempty"` + ExpiresAt int64 `json:"expires_at"` + StartedAt *int64 `json:"started_at,omitempty"` + CancelledAt *int64 `json:"cancelled_at,omitempty"` + FailedAt *int64 `json:"failed_at,omitempty"` + CompletedAt *int64 `json:"completed_at,omitempty"` + Model string `json:"model"` + Instructions string `json:"instructions,omitempty"` + Tools []Tool `json:"tools"` + FileIDS []string `json:"file_ids"` //nolint:revive // backwards-compatibility + Metadata map[string]any `json:"metadata"` + Usage Usage `json:"usage,omitempty"` + + Temperature *float32 `json:"temperature,omitempty"` + // The maximum number of prompt tokens that may be used over the course of the run. + // If the run exceeds the number of prompt tokens specified, the run will end with status 'incomplete'. + MaxPromptTokens int `json:"max_prompt_tokens,omitempty"` + // The maximum number of completion tokens that may be used over the course of the run. + // If the run exceeds the number of completion tokens specified, the run will end with status 'incomplete'. + MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` + // ThreadTruncationStrategy defines the truncation strategy to use for the thread. + TruncationStrategy *ThreadTruncationStrategy `json:"truncation_strategy,omitempty"` + + httpHeader +} + +type RunStatus string + +const ( + RunStatusQueued RunStatus = "queued" + RunStatusInProgress RunStatus = "in_progress" + RunStatusRequiresAction RunStatus = "requires_action" + RunStatusCancelling RunStatus = "cancelling" + RunStatusFailed RunStatus = "failed" + RunStatusCompleted RunStatus = "completed" + RunStatusIncomplete RunStatus = "incomplete" + RunStatusExpired RunStatus = "expired" + RunStatusCancelled RunStatus = "cancelled" +) + +type RunRequiredAction struct { + Type RequiredActionType `json:"type"` + SubmitToolOutputs *SubmitToolOutputs `json:"submit_tool_outputs,omitempty"` +} + +type RequiredActionType string + +const ( + RequiredActionTypeSubmitToolOutputs RequiredActionType = "submit_tool_outputs" +) + +type SubmitToolOutputs struct { + ToolCalls []ToolCall `json:"tool_calls"` +} + +type RunLastError struct { + Code RunError `json:"code"` + Message string `json:"message"` +} + +type RunError string + +const ( + RunErrorServerError RunError = "server_error" + RunErrorRateLimitExceeded RunError = "rate_limit_exceeded" +) + +type RunRequest struct { + AssistantID string `json:"assistant_id"` + Model string `json:"model,omitempty"` + Instructions string `json:"instructions,omitempty"` + AdditionalInstructions string `json:"additional_instructions,omitempty"` + AdditionalMessages []ThreadMessage `json:"additional_messages,omitempty"` + Tools []Tool `json:"tools,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` + + // Sampling temperature between 0 and 2. Higher values like 0.8 are more random. + // lower values are more focused and deterministic. + Temperature *float32 `json:"temperature,omitempty"` + TopP *float32 `json:"top_p,omitempty"` + + // The maximum number of prompt tokens that may be used over the course of the run. + // If the run exceeds the number of prompt tokens specified, the run will end with status 'incomplete'. + MaxPromptTokens int `json:"max_prompt_tokens,omitempty"` + + // The maximum number of completion tokens that may be used over the course of the run. + // If the run exceeds the number of completion tokens specified, the run will end with status 'incomplete'. + MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` + + // ThreadTruncationStrategy defines the truncation strategy to use for the thread. + TruncationStrategy *ThreadTruncationStrategy `json:"truncation_strategy,omitempty"` + + // This can be either a string or a ToolChoice object. + ToolChoice any `json:"tool_choice,omitempty"` + // This can be either a string or a ResponseFormat object. + ResponseFormat any `json:"response_format,omitempty"` + // Disable the default behavior of parallel tool calls by setting it: false. + ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` +} + +// ThreadTruncationStrategy defines the truncation strategy to use for the thread. +// https://platform.openai.com/docs/assistants/how-it-works/truncation-strategy. +type ThreadTruncationStrategy struct { + // default 'auto'. + Type TruncationStrategy `json:"type,omitempty"` + // this field should be set if the truncation strategy is set to LastMessages. + LastMessages *int `json:"last_messages,omitempty"` +} + +// TruncationStrategy defines the existing truncation strategies existing for thread management in an assistant. +type TruncationStrategy string + +const ( + // TruncationStrategyAuto messages in the middle of the thread will be dropped to fit the context length of the model. + TruncationStrategyAuto = TruncationStrategy("auto") + // TruncationStrategyLastMessages the thread will be truncated to the n most recent messages in the thread. + TruncationStrategyLastMessages = TruncationStrategy("last_messages") +) + +// ReponseFormat specifies the format the model must output. +// https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-response_format. +// Type can either be text or json_object. +type ReponseFormat struct { + Type string `json:"type"` +} + +type RunModifyRequest struct { + Metadata map[string]any `json:"metadata,omitempty"` +} + +// RunList is a list of runs. +type RunList struct { + Runs []Run `json:"data"` + + httpHeader +} + +type SubmitToolOutputsRequest struct { + ToolOutputs []ToolOutput `json:"tool_outputs"` +} + +type ToolOutput struct { + ToolCallID string `json:"tool_call_id"` + Output any `json:"output"` +} + +type CreateThreadAndRunRequest struct { + RunRequest + Thread ThreadRequest `json:"thread"` +} + +type RunStep struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + AssistantID string `json:"assistant_id"` + ThreadID string `json:"thread_id"` + RunID string `json:"run_id"` + Type RunStepType `json:"type"` + Status RunStepStatus `json:"status"` + StepDetails StepDetails `json:"step_details"` + LastError *RunLastError `json:"last_error,omitempty"` + ExpiredAt *int64 `json:"expired_at,omitempty"` + CancelledAt *int64 `json:"cancelled_at,omitempty"` + FailedAt *int64 `json:"failed_at,omitempty"` + CompletedAt *int64 `json:"completed_at,omitempty"` + Metadata map[string]any `json:"metadata"` + + httpHeader +} + +type RunStepStatus string + +const ( + RunStepStatusInProgress RunStepStatus = "in_progress" + RunStepStatusCancelling RunStepStatus = "cancelled" + RunStepStatusFailed RunStepStatus = "failed" + RunStepStatusCompleted RunStepStatus = "completed" + RunStepStatusExpired RunStepStatus = "expired" +) + +type RunStepType string + +const ( + RunStepTypeMessageCreation RunStepType = "message_creation" + RunStepTypeToolCalls RunStepType = "tool_calls" +) + +type StepDetails struct { + Type RunStepType `json:"type"` + MessageCreation *StepDetailsMessageCreation `json:"message_creation,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` +} + +type StepDetailsMessageCreation struct { + MessageID string `json:"message_id"` +} + +// RunStepList is a list of steps. +type RunStepList struct { + RunSteps []RunStep `json:"data"` + + FirstID string `json:"first_id"` + LastID string `json:"last_id"` + HasMore bool `json:"has_more"` + + httpHeader +} + +type Pagination struct { + Limit *int + Order *string + After *string + Before *string +} + +// CreateRun creates a new run. +func (c *Client) CreateRun( + ctx context.Context, + threadID string, + request RunRequest, +) (response Run, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs", threadID) + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveRun retrieves a run. +func (c *Client) RetrieveRun( + ctx context.Context, + threadID string, + runID string, +) (response Run, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s", threadID, runID) + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ModifyRun modifies a run. +func (c *Client) ModifyRun( + ctx context.Context, + threadID string, + runID string, + request RunModifyRequest, +) (response Run, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s", threadID, runID) + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ListRuns lists runs. +func (c *Client) ListRuns( + ctx context.Context, + threadID string, + pagination Pagination, +) (response RunList, err error) { + urlValues := url.Values{} + if pagination.Limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) + } + if pagination.Order != nil { + urlValues.Add("order", *pagination.Order) + } + if pagination.After != nil { + urlValues.Add("after", *pagination.After) + } + if pagination.Before != nil { + urlValues.Add("before", *pagination.Before) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("/threads/%s/runs%s", threadID, encodedValues) + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// SubmitToolOutputs submits tool outputs. +func (c *Client) SubmitToolOutputs( + ctx context.Context, + threadID string, + runID string, + request SubmitToolOutputsRequest) (response Run, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/submit_tool_outputs", threadID, runID) + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// CancelRun cancels a run. +func (c *Client) CancelRun( + ctx context.Context, + threadID string, + runID string) (response Run, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/cancel", threadID, runID) + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// CreateThreadAndRun submits tool outputs. +func (c *Client) CreateThreadAndRun( + ctx context.Context, + request CreateThreadAndRunRequest) (response Run, err error) { + urlSuffix := "/threads/runs" + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveRunStep retrieves a run step. +func (c *Client) RetrieveRunStep( + ctx context.Context, + threadID string, + runID string, + stepID string, +) (response RunStep, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/steps/%s", threadID, runID, stepID) + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ListRunSteps lists run steps. +func (c *Client) ListRunSteps( + ctx context.Context, + threadID string, + runID string, + pagination Pagination, +) (response RunStepList, err error) { + urlValues := url.Values{} + if pagination.Limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) + } + if pagination.Order != nil { + urlValues.Add("order", *pagination.Order) + } + if pagination.After != nil { + urlValues.Add("after", *pagination.After) + } + if pagination.Before != nil { + urlValues.Add("before", *pagination.Before) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/steps%s", threadID, runID, encodedValues) + req, err := c.newRequest( + ctx, + http.MethodGet, + c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/speech.go b/backend/vendor/github.com/sashabaranov/go-openai/speech.go new file mode 100644 index 0000000..20b52e3 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/speech.go @@ -0,0 +1,59 @@ +package openai + +import ( + "context" + "net/http" +) + +type SpeechModel string + +const ( + TTSModel1 SpeechModel = "tts-1" + TTSModel1HD SpeechModel = "tts-1-hd" + TTSModelCanary SpeechModel = "canary-tts" +) + +type SpeechVoice string + +const ( + VoiceAlloy SpeechVoice = "alloy" + VoiceEcho SpeechVoice = "echo" + VoiceFable SpeechVoice = "fable" + VoiceOnyx SpeechVoice = "onyx" + VoiceNova SpeechVoice = "nova" + VoiceShimmer SpeechVoice = "shimmer" +) + +type SpeechResponseFormat string + +const ( + SpeechResponseFormatMp3 SpeechResponseFormat = "mp3" + SpeechResponseFormatOpus SpeechResponseFormat = "opus" + SpeechResponseFormatAac SpeechResponseFormat = "aac" + SpeechResponseFormatFlac SpeechResponseFormat = "flac" + SpeechResponseFormatWav SpeechResponseFormat = "wav" + SpeechResponseFormatPcm SpeechResponseFormat = "pcm" +) + +type CreateSpeechRequest struct { + Model SpeechModel `json:"model"` + Input string `json:"input"` + Voice SpeechVoice `json:"voice"` + ResponseFormat SpeechResponseFormat `json:"response_format,omitempty"` // Optional, default to mp3 + Speed float64 `json:"speed,omitempty"` // Optional, default to 1.0 +} + +func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response RawResponse, err error) { + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL("/audio/speech", withModel(string(request.Model))), + withBody(request), + withContentType("application/json"), + ) + if err != nil { + return + } + + return c.sendRequestRaw(req) +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/stream.go b/backend/vendor/github.com/sashabaranov/go-openai/stream.go new file mode 100644 index 0000000..a61c7c9 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/stream.go @@ -0,0 +1,55 @@ +package openai + +import ( + "context" + "errors" + "net/http" +) + +var ( + ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages") +) + +type CompletionStream struct { + *streamReader[CompletionResponse] +} + +// CreateCompletionStream — API call to create a completion w/ streaming +// support. It sets whether to stream back partial progress. If set, tokens will be +// sent as data-only server-sent events as they become available, with the +// stream terminated by a data: [DONE] message. +func (c *Client) CreateCompletionStream( + ctx context.Context, + request CompletionRequest, +) (stream *CompletionStream, err error) { + urlSuffix := "/completions" + if !checkEndpointSupportsModel(urlSuffix, request.Model) { + err = ErrCompletionUnsupportedModel + return + } + + if !checkPromptType(request.Prompt) { + err = ErrCompletionRequestPromptTypeNotSupported + return + } + + request.Stream = true + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(urlSuffix, withModel(request.Model)), + withBody(request), + ) + if err != nil { + return nil, err + } + + resp, err := sendRequestStream[CompletionResponse](c, req) + if err != nil { + return + } + stream = &CompletionStream{ + streamReader: resp, + } + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/stream_reader.go b/backend/vendor/github.com/sashabaranov/go-openai/stream_reader.go new file mode 100644 index 0000000..ecfa268 --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/stream_reader.go @@ -0,0 +1,118 @@ +package openai + +import ( + "bufio" + "bytes" + "fmt" + "io" + "net/http" + + utils "github.com/sashabaranov/go-openai/internal" +) + +var ( + headerData = []byte("data: ") + errorPrefix = []byte(`data: {"error":`) +) + +type streamable interface { + ChatCompletionStreamResponse | CompletionResponse +} + +type streamReader[T streamable] struct { + emptyMessagesLimit uint + isFinished bool + + reader *bufio.Reader + response *http.Response + errAccumulator utils.ErrorAccumulator + unmarshaler utils.Unmarshaler + + httpHeader +} + +func (stream *streamReader[T]) Recv() (response T, err error) { + rawLine, err := stream.RecvRaw() + if err != nil { + return + } + + err = stream.unmarshaler.Unmarshal(rawLine, &response) + if err != nil { + return + } + return response, nil +} + +func (stream *streamReader[T]) RecvRaw() ([]byte, error) { + if stream.isFinished { + return nil, io.EOF + } + + return stream.processLines() +} + +//nolint:gocognit +func (stream *streamReader[T]) processLines() ([]byte, error) { + var ( + emptyMessagesCount uint + hasErrorPrefix bool + ) + + for { + rawLine, readErr := stream.reader.ReadBytes('\n') + if readErr != nil || hasErrorPrefix { + respErr := stream.unmarshalError() + if respErr != nil { + return nil, fmt.Errorf("error, %w", respErr.Error) + } + return nil, readErr + } + + noSpaceLine := bytes.TrimSpace(rawLine) + if bytes.HasPrefix(noSpaceLine, errorPrefix) { + hasErrorPrefix = true + } + if !bytes.HasPrefix(noSpaceLine, headerData) || hasErrorPrefix { + if hasErrorPrefix { + noSpaceLine = bytes.TrimPrefix(noSpaceLine, headerData) + } + writeErr := stream.errAccumulator.Write(noSpaceLine) + if writeErr != nil { + return nil, writeErr + } + emptyMessagesCount++ + if emptyMessagesCount > stream.emptyMessagesLimit { + return nil, ErrTooManyEmptyStreamMessages + } + + continue + } + + noPrefixLine := bytes.TrimPrefix(noSpaceLine, headerData) + if string(noPrefixLine) == "[DONE]" { + stream.isFinished = true + return nil, io.EOF + } + + return noPrefixLine, nil + } +} + +func (stream *streamReader[T]) unmarshalError() (errResp *ErrorResponse) { + errBytes := stream.errAccumulator.Bytes() + if len(errBytes) == 0 { + return + } + + err := stream.unmarshaler.Unmarshal(errBytes, &errResp) + if err != nil { + errResp = nil + } + + return +} + +func (stream *streamReader[T]) Close() error { + return stream.response.Body.Close() +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/thread.go b/backend/vendor/github.com/sashabaranov/go-openai/thread.go new file mode 100644 index 0000000..bc08e2b --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/thread.go @@ -0,0 +1,171 @@ +package openai + +import ( + "context" + "net/http" +) + +const ( + threadsSuffix = "/threads" +) + +type Thread struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Metadata map[string]any `json:"metadata"` + ToolResources ToolResources `json:"tool_resources,omitempty"` + + httpHeader +} + +type ThreadRequest struct { + Messages []ThreadMessage `json:"messages,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` + ToolResources *ToolResourcesRequest `json:"tool_resources,omitempty"` +} + +type ToolResources struct { + CodeInterpreter *CodeInterpreterToolResources `json:"code_interpreter,omitempty"` + FileSearch *FileSearchToolResources `json:"file_search,omitempty"` +} + +type CodeInterpreterToolResources struct { + FileIDs []string `json:"file_ids,omitempty"` +} + +type FileSearchToolResources struct { + VectorStoreIDs []string `json:"vector_store_ids,omitempty"` +} + +type ToolResourcesRequest struct { + CodeInterpreter *CodeInterpreterToolResourcesRequest `json:"code_interpreter,omitempty"` + FileSearch *FileSearchToolResourcesRequest `json:"file_search,omitempty"` +} + +type CodeInterpreterToolResourcesRequest struct { + FileIDs []string `json:"file_ids,omitempty"` +} + +type FileSearchToolResourcesRequest struct { + VectorStoreIDs []string `json:"vector_store_ids,omitempty"` + VectorStores []VectorStoreToolResources `json:"vector_stores,omitempty"` +} + +type VectorStoreToolResources struct { + FileIDs []string `json:"file_ids,omitempty"` + ChunkingStrategy *ChunkingStrategy `json:"chunking_strategy,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` +} + +type ChunkingStrategy struct { + Type ChunkingStrategyType `json:"type"` + Static *StaticChunkingStrategy `json:"static,omitempty"` +} + +type StaticChunkingStrategy struct { + MaxChunkSizeTokens int `json:"max_chunk_size_tokens"` + ChunkOverlapTokens int `json:"chunk_overlap_tokens"` +} + +type ChunkingStrategyType string + +const ( + ChunkingStrategyTypeAuto ChunkingStrategyType = "auto" + ChunkingStrategyTypeStatic ChunkingStrategyType = "static" +) + +type ModifyThreadRequest struct { + Metadata map[string]any `json:"metadata"` + ToolResources *ToolResources `json:"tool_resources,omitempty"` +} + +type ThreadMessageRole string + +const ( + ThreadMessageRoleAssistant ThreadMessageRole = "assistant" + ThreadMessageRoleUser ThreadMessageRole = "user" +) + +type ThreadMessage struct { + Role ThreadMessageRole `json:"role"` + Content string `json:"content"` + FileIDs []string `json:"file_ids,omitempty"` + Attachments []ThreadAttachment `json:"attachments,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` +} + +type ThreadAttachment struct { + FileID string `json:"file_id"` + Tools []ThreadAttachmentTool `json:"tools"` +} + +type ThreadAttachmentTool struct { + Type string `json:"type"` +} + +type ThreadDeleteResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +// CreateThread creates a new thread. +func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (response Thread, err error) { + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(threadsSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// RetrieveThread retrieves a thread. +func (c *Client) RetrieveThread(ctx context.Context, threadID string) (response Thread, err error) { + urlSuffix := threadsSuffix + "/" + threadID + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// ModifyThread modifies a thread. +func (c *Client) ModifyThread( + ctx context.Context, + threadID string, + request ModifyThreadRequest, +) (response Thread, err error) { + urlSuffix := threadsSuffix + "/" + threadID + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} + +// DeleteThread deletes a thread. +func (c *Client) DeleteThread( + ctx context.Context, + threadID string, +) (response ThreadDeleteResponse, err error) { + urlSuffix := threadsSuffix + "/" + threadID + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/github.com/sashabaranov/go-openai/vector_store.go b/backend/vendor/github.com/sashabaranov/go-openai/vector_store.go new file mode 100644 index 0000000..682bb1c --- /dev/null +++ b/backend/vendor/github.com/sashabaranov/go-openai/vector_store.go @@ -0,0 +1,348 @@ +package openai + +import ( + "context" + "fmt" + "net/http" + "net/url" +) + +const ( + vectorStoresSuffix = "/vector_stores" + vectorStoresFilesSuffix = "/files" + vectorStoresFileBatchesSuffix = "/file_batches" +) + +type VectorStoreFileCount struct { + InProgress int `json:"in_progress"` + Completed int `json:"completed"` + Failed int `json:"failed"` + Cancelled int `json:"cancelled"` + Total int `json:"total"` +} + +type VectorStore struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Name string `json:"name"` + UsageBytes int `json:"usage_bytes"` + FileCounts VectorStoreFileCount `json:"file_counts"` + Status string `json:"status"` + ExpiresAfter *VectorStoreExpires `json:"expires_after"` + ExpiresAt *int `json:"expires_at"` + Metadata map[string]any `json:"metadata"` + + httpHeader +} + +type VectorStoreExpires struct { + Anchor string `json:"anchor"` + Days int `json:"days"` +} + +// VectorStoreRequest provides the vector store request parameters. +type VectorStoreRequest struct { + Name string `json:"name,omitempty"` + FileIDs []string `json:"file_ids,omitempty"` + ExpiresAfter *VectorStoreExpires `json:"expires_after,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` +} + +// VectorStoresList is a list of vector store. +type VectorStoresList struct { + VectorStores []VectorStore `json:"data"` + LastID *string `json:"last_id"` + FirstID *string `json:"first_id"` + HasMore bool `json:"has_more"` + httpHeader +} + +type VectorStoreDeleteResponse struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + +type VectorStoreFile struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + VectorStoreID string `json:"vector_store_id"` + UsageBytes int `json:"usage_bytes"` + Status string `json:"status"` + + httpHeader +} + +type VectorStoreFileRequest struct { + FileID string `json:"file_id"` +} + +type VectorStoreFilesList struct { + VectorStoreFiles []VectorStoreFile `json:"data"` + FirstID *string `json:"first_id"` + LastID *string `json:"last_id"` + HasMore bool `json:"has_more"` + + httpHeader +} + +type VectorStoreFileBatch struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + VectorStoreID string `json:"vector_store_id"` + Status string `json:"status"` + FileCounts VectorStoreFileCount `json:"file_counts"` + + httpHeader +} + +type VectorStoreFileBatchRequest struct { + FileIDs []string `json:"file_ids"` +} + +// CreateVectorStore creates a new vector store. +func (c *Client) CreateVectorStore(ctx context.Context, request VectorStoreRequest) (response VectorStore, err error) { + req, _ := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(vectorStoresSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion), + ) + + err = c.sendRequest(req, &response) + return +} + +// RetrieveVectorStore retrieves an vector store. +func (c *Client) RetrieveVectorStore( + ctx context.Context, + vectorStoreID string, +) (response VectorStore, err error) { + urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// ModifyVectorStore modifies a vector store. +func (c *Client) ModifyVectorStore( + ctx context.Context, + vectorStoreID string, + request VectorStoreRequest, +) (response VectorStore, err error) { + urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) + req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// DeleteVectorStore deletes an vector store. +func (c *Client) DeleteVectorStore( + ctx context.Context, + vectorStoreID string, +) (response VectorStoreDeleteResponse, err error) { + urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) + req, _ := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// ListVectorStores Lists the currently available vector store. +func (c *Client) ListVectorStores( + ctx context.Context, + pagination Pagination, +) (response VectorStoresList, err error) { + urlValues := url.Values{} + + if pagination.After != nil { + urlValues.Add("after", *pagination.After) + } + if pagination.Order != nil { + urlValues.Add("order", *pagination.Order) + } + if pagination.Limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) + } + if pagination.Before != nil { + urlValues.Add("before", *pagination.Before) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s%s", vectorStoresSuffix, encodedValues) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// CreateVectorStoreFile creates a new vector store file. +func (c *Client) CreateVectorStoreFile( + ctx context.Context, + vectorStoreID string, + request VectorStoreFileRequest, +) (response VectorStoreFile, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix) + req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// RetrieveVectorStoreFile retrieves a vector store file. +func (c *Client) RetrieveVectorStoreFile( + ctx context.Context, + vectorStoreID string, + fileID string, +) (response VectorStoreFile, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, fileID) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// DeleteVectorStoreFile deletes an existing file. +func (c *Client) DeleteVectorStoreFile( + ctx context.Context, + vectorStoreID string, + fileID string, +) (err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, fileID) + req, _ := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, nil) + return +} + +// ListVectorStoreFiles Lists the currently available files for a vector store. +func (c *Client) ListVectorStoreFiles( + ctx context.Context, + vectorStoreID string, + pagination Pagination, +) (response VectorStoreFilesList, err error) { + urlValues := url.Values{} + if pagination.After != nil { + urlValues.Add("after", *pagination.After) + } + if pagination.Limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) + } + if pagination.Before != nil { + urlValues.Add("before", *pagination.Before) + } + if pagination.Order != nil { + urlValues.Add("order", *pagination.Order) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s/%s%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, encodedValues) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// CreateVectorStoreFileBatch creates a new vector store file batch. +func (c *Client) CreateVectorStoreFileBatch( + ctx context.Context, + vectorStoreID string, + request VectorStoreFileBatchRequest, +) (response VectorStoreFileBatch, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix) + req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), + withBody(request), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// RetrieveVectorStoreFileBatch retrieves a vector store file batch. +func (c *Client) RetrieveVectorStoreFileBatch( + ctx context.Context, + vectorStoreID string, + batchID string, +) (response VectorStoreFileBatch, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix, batchID) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// CancelVectorStoreFileBatch cancel a new vector store file batch. +func (c *Client) CancelVectorStoreFileBatch( + ctx context.Context, + vectorStoreID string, + batchID string, +) (response VectorStoreFileBatch, err error) { + urlSuffix := fmt.Sprintf("%s/%s%s/%s%s", vectorStoresSuffix, + vectorStoreID, vectorStoresFileBatchesSuffix, batchID, "/cancel") + req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} + +// ListVectorStoreFiles Lists the currently available files for a vector store. +func (c *Client) ListVectorStoreFilesInBatch( + ctx context.Context, + vectorStoreID string, + batchID string, + pagination Pagination, +) (response VectorStoreFilesList, err error) { + urlValues := url.Values{} + if pagination.After != nil { + urlValues.Add("after", *pagination.After) + } + if pagination.Limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) + } + if pagination.Before != nil { + urlValues.Add("before", *pagination.Before) + } + if pagination.Order != nil { + urlValues.Add("order", *pagination.Order) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s/%s%s/%s%s%s", vectorStoresSuffix, + vectorStoreID, vectorStoresFileBatchesSuffix, batchID, "/files", encodedValues) + req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + + err = c.sendRequest(req, &response) + return +} diff --git a/backend/vendor/modules.txt b/backend/vendor/modules.txt index a8d2178..f83fd81 100644 --- a/backend/vendor/modules.txt +++ b/backend/vendor/modules.txt @@ -45,6 +45,10 @@ github.com/montanaflynn/stats # github.com/pmezard/go-difflib v1.0.0 ## explicit github.com/pmezard/go-difflib/difflib +# github.com/sashabaranov/go-openai v1.38.2 +## explicit; go 1.18 +github.com/sashabaranov/go-openai +github.com/sashabaranov/go-openai/internal # github.com/stretchr/testify v1.10.0 ## explicit; go 1.17 github.com/stretchr/testify/assert diff --git a/backend/web/components/preview-item.gohtml b/backend/web/components/preview-item.gohtml index 69970ca..0092737 100644 --- a/backend/web/components/preview-item.gohtml +++ b/backend/web/components/preview-item.gohtml @@ -3,6 +3,11 @@
Анонс:

{{.Excerpt}}

+ {{if .Summary}} +
Сводка:
+

{{.Summary}}

+ {{end}} +
Полный контент с тегами:
{{.Rich }}
diff --git a/docker-compose.yml b/docker-compose.yml index 06122d5..f9c547b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: - MONGO_URI=mongodb://root:Squid3kIc6Dew4ad8Ci5@mongo:27017 - MONGO_DELAY=10s - CREDS=admin:jpm6AQH!kbx2tvk!fqc - #- OPENAI_KEY=insert_key_here + #- OPENAI_API_KEY=insert_key_here ports: - "8080:8080"