Skip to content

Commit

Permalink
Add OpenRouter support and improve logging in chat handler (#601)
Browse files Browse the repository at this point in the history
- Added OpenRouter API URL handling in `llm_openai.go`.
- Enhanced logging in `chat_main_handler.go` for better debugging:
  - Added detailed config logging.
  - Added pre-request and stream error logging.
  • Loading branch information
swuecho authored Feb 9, 2025
1 parent e95bef6 commit 71c3556
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ func (h *ChatHandler) chatStream(w http.ResponseWriter, chatSession sqlc_queries
}

config, err := genOpenAIConfig(chatModel)
log.Printf("%+v", config)
log.Printf("%+v", config.String())
// print all config details
if err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "gen open ai config").Error(), err)
return nil, err
Expand Down Expand Up @@ -533,6 +534,7 @@ func (h *ChatHandler) chatStream(w http.ResponseWriter, chatSession sqlc_queries
fmt.Fprint(w, string(data))
return &models.LLMAnswer{Answer: completion.Choices[0].Message.Content, AnswerId: completion.ID}, nil
}
log.Print("before request")
stream, err := client.CreateChatCompletionStream(ctx, openai_req)

if err != nil {
Expand Down Expand Up @@ -566,6 +568,7 @@ func (h *ChatHandler) chatStream(w http.ResponseWriter, chatSession sqlc_queries
for {
rawLine, err := stream.RecvRaw()
if err != nil {
log.Printf("stream error: %+v", err)
if errors.Is(err, io.EOF) {
// send the last message
if len(answer) > 0 {
Expand Down
10 changes: 10 additions & 0 deletions api/llm_openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ func getModelBaseUrl(apiUrl string) (string, error) {
if apiUrl == "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" {
return "https://dashscope.aliyuncs.com/compatible-mode/v1", nil
}
// open router
// https://openrouter.ai/api/v1
if strings.Contains(apiUrl, "openrouter") {
// keep the url until /v1
slashIndex := strings.Index(apiUrl, "/v1")
if slashIndex > 0 {
return apiUrl[:slashIndex] + "/v1", nil
}
return apiUrl, nil
}
parsedUrl, err := url.Parse(apiUrl)
if err != nil {
return "", err
Expand Down

0 comments on commit 71c3556

Please sign in to comment.