Skip to content

Commit adf8216

Browse files
committed
todo: fix gemini bad request
1 parent f693cf4 commit adf8216

21 files changed

Lines changed: 113 additions & 110 deletions

File tree

internal/api/chat/create_conversation_message_stream_v2.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package chat
22

33
import (
44
"context"
5+
"fmt"
56
"paperdebugger/internal/api/mapper"
67
"paperdebugger/internal/libs/contextutil"
78
"paperdebugger/internal/libs/shared"
89
"paperdebugger/internal/models"
910
"paperdebugger/internal/services"
1011
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
12+
"strings"
1113

1214
"github.com/google/uuid"
1315
"github.com/openai/openai-go/v3"
@@ -276,13 +278,38 @@ func (s *ChatServerV2) CreateConversationMessageStream(
276278
return s.sendStreamError(stream, err)
277279
}
278280

281+
// Check if user has an API key for requested model
282+
var customModel *models.CustomModel
283+
customModel = nil
284+
for _, m := range settings.CustomModels {
285+
if m.Slug == modelSlug {
286+
customModel = &m
287+
break
288+
}
289+
}
290+
279291
// Usage is the same as ChatCompletion, just passing the stream parameter
280-
llmProvider := &models.LLMProviderConfig{
281-
APIKey: settings.OpenAIAPIKey,
292+
var llmProvider *models.LLMProviderConfig
293+
if customModel == nil {
294+
// User did not specify API key for this model
295+
llmProvider = &models.LLMProviderConfig{
296+
APIKey: settings.OpenAIAPIKey,
297+
}
298+
} else {
299+
modelSlug = modelSlug[strings.Index(modelSlug, "/")+1:]
300+
llmProvider = &models.LLMProviderConfig{
301+
APIKey: customModel.APIKey,
302+
Endpoint: customModel.BaseUrl,
303+
}
282304
}
283305

306+
fmt.Println(modelSlug)
307+
fmt.Println(llmProvider.Endpoint)
308+
fmt.Println(llmProvider.APIKey)
309+
fmt.Println("************************")
284310
openaiChatHistory, inappChatHistory, err := s.aiClientV2.ChatCompletionStreamV2(ctx, stream, conversation.ID.Hex(), modelSlug, conversation.OpenaiChatHistoryCompletion, llmProvider)
285311
if err != nil {
312+
fmt.Println(err)
286313
return s.sendStreamError(stream, err)
287314
}
288315

internal/api/chat/list_supported_models_v2.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chat
22

33
import (
44
"context"
5-
"strings"
65

76
"paperdebugger/internal/libs/contextutil"
87
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
@@ -227,19 +226,19 @@ func (s *ChatServerV2) ListSupportedModels(
227226
for _, model := range settings.CustomModels {
228227
if model.Slug == config.slugOpenRouter {
229228
// User has API key for this model, use slugOpenAI instead of slugOpenRouter if applicable
230-
slug := config.slugOpenRouter
231-
if strings.TrimSpace(config.slugOpenAI) != "" {
232-
slug = config.slugOpenAI
233-
}
229+
// slug := config.slugOpenRouter
230+
// if strings.TrimSpace(config.slugOpenAI) != "" {
231+
// slug = config.slugOpenAI
232+
// }
234233

235234
models = append(models, &chatv2.SupportedModel{
236-
Name: model.Name,
237-
Slug: slug,
238-
TotalContext: int64(model.ContextWindow),
239-
MaxOutput: int64(model.MaxOutput),
240-
InputPrice: int64(model.InputPrice),
241-
OutputPrice: int64(model.OutputPrice),
242-
CustomModelId: model.ID.Hex(),
235+
Name: model.Name,
236+
Slug: model.Slug,
237+
TotalContext: int64(model.ContextWindow),
238+
MaxOutput: int64(model.MaxOutput),
239+
InputPrice: int64(model.InputPrice),
240+
OutputPrice: int64(model.OutputPrice),
241+
IsCustom: true,
243242
})
244243
hasOwnAPIKey = true
245244
continue
@@ -266,6 +265,7 @@ func (s *ChatServerV2) ListSupportedModels(
266265
MaxOutput: config.maxOutput,
267266
InputPrice: config.inputPrice,
268267
OutputPrice: config.outputPrice,
268+
IsCustom: false,
269269
}
270270

271271
// If model requires own key but user hasn't provided one, mark as disabled

internal/api/mapper/user.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@ package mapper
33
import (
44
"paperdebugger/internal/models"
55
userv1 "paperdebugger/pkg/gen/api/user/v1"
6-
7-
"go.mongodb.org/mongo-driver/v2/bson"
86
)
97

108
func MapProtoSettingsToModel(settings *userv1.Settings) *models.Settings {
119
// Map the slice of custom models
1210
customModels := make([]models.CustomModel, len(settings.CustomModels))
1311
for i, m := range settings.CustomModels {
14-
id, _ := bson.ObjectIDFromHex(m.Id)
15-
if m.Id == "" {
16-
// No ID provided, new custom model
17-
id = bson.NewObjectID()
18-
}
19-
2012
customModels[i] = models.CustomModel{
21-
ID: id,
13+
Slug: m.Slug,
2214
Name: m.Name,
2315
BaseUrl: m.BaseUrl,
24-
Slug: m.Slug,
2516
APIKey: m.ApiKey,
2617
ContextWindow: m.ContextWindow,
2718
MaxOutput: m.MaxOutput,
@@ -46,10 +37,9 @@ func MapModelSettingsToProto(settings *models.Settings) *userv1.Settings {
4637
customModels := make([]*userv1.CustomModel, len(settings.CustomModels))
4738
for i, m := range settings.CustomModels {
4839
customModels[i] = &userv1.CustomModel{
49-
Id: m.ID.Hex(),
40+
Slug: m.Slug,
5041
Name: m.Name,
5142
BaseUrl: m.BaseUrl,
52-
Slug: m.Slug,
5343
ApiKey: m.APIKey,
5444
ContextWindow: m.ContextWindow,
5545
InputPrice: m.InputPrice,

internal/models/user.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package models
33
import "go.mongodb.org/mongo-driver/v2/bson"
44

55
type CustomModel struct {
6-
ID bson.ObjectID `bson:"_id"`
7-
Name string `bson:"name"`
8-
BaseUrl string `bson:"base_url"`
9-
Slug string `bson:"slug"`
10-
APIKey string `bson:"api_key"`
11-
ContextWindow int32 `bson:"context_window"`
12-
MaxOutput int32 `bson:"max_output"`
13-
InputPrice int32 `bson:"input_price"`
14-
OutputPrice int32 `bson:"output_price"`
6+
Slug string `bson:"_id"`
7+
Name string `bson:"name"`
8+
BaseUrl string `bson:"base_url"`
9+
APIKey string `bson:"api_key"`
10+
ContextWindow int32 `bson:"context_window"`
11+
MaxOutput int32 `bson:"max_output"`
12+
InputPrice int32 `bson:"input_price"`
13+
OutputPrice int32 `bson:"output_price"`
1514
}
1615

1716
type Settings struct {

internal/services/toolkit/client/client_v2.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"fmt"
45
"paperdebugger/internal/libs/cfg"
56
"paperdebugger/internal/libs/db"
67
"paperdebugger/internal/libs/logger"
@@ -46,6 +47,9 @@ func (a *AIClientV2) GetOpenAIClient(llmConfig *models.LLMProviderConfig) *opena
4647
APIKey = a.cfg.InferenceAPIKey
4748
}
4849

50+
fmt.Println(Endpoint)
51+
fmt.Println(APIKey)
52+
4953
opts := []option.RequestOption{
5054
option.WithAPIKey(APIKey),
5155
option.WithBaseURL(Endpoint),

internal/services/toolkit/client/completion_v2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"paperdebugger/internal/models"
78
"paperdebugger/internal/services/toolkit/handler"
89
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
@@ -68,6 +69,8 @@ func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream
6869
oaiClient := a.GetOpenAIClient(llmProvider)
6970
params := getDefaultParamsV2(modelSlug, a.toolCallHandler.Registry)
7071

72+
fmt.Println(params)
73+
7174
for {
7275
params.Messages = openaiChatHistory
7376
// var openaiOutput OpenAIChatHistory

pkg/gen/api/auth/v1/auth_grpc.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gen/api/chat/v1/chat_grpc.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gen/api/chat/v2/chat.pb.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gen/api/chat/v2/chat_grpc.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)