Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ overleaf.kubeconfig
# coverage report
coverage.out
coverage.html

# claude code
CLAUDE.md
43 changes: 43 additions & 0 deletions internal/api/chat/get_citation_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package chat

import (
"context"

"paperdebugger/internal/libs/contextutil"
"paperdebugger/internal/models"
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
)

func (s *ChatServerV2) GetCitationKeys(
ctx context.Context,
req *chatv2.GetCitationKeysRequest,
) (*chatv2.GetCitationKeysResponse, error) {
actor, err := contextutil.GetActor(ctx)
if err != nil {
return nil, err
}

settings, err := s.userService.GetUserSettings(ctx, actor.ID)
if err != nil {
return nil, err
}

llmProvider := &models.LLMProviderConfig{
APIKey: settings.OpenAIAPIKey,
}

citationKeys, err := s.aiClientV2.GetCitationKeys(
ctx,
req.GetSentence(),
actor.ID,
req.GetProjectId(),
llmProvider,
)
if err != nil {
return nil, err
}

return &chatv2.GetCitationKeysResponse{
CitationKeys: citationKeys,
}, nil
}
53 changes: 53 additions & 0 deletions internal/services/toolkit/client/get_citation_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package client

// TODO: This file should not place in the client package.
import (
"context"
"fmt"
"paperdebugger/internal/models"
"strings"

"github.com/openai/openai-go/v3"
"go.mongodb.org/mongo-driver/v2/bson"
)

func (a *AIClientV2) GetCitationKeys(ctx context.Context, sentence string, userId bson.ObjectID, projectId string, llmProvider *models.LLMProviderConfig) (string, error) {
// Get bibliography from mongodb
project, err := a.projectService.GetProject(ctx, userId, projectId)
if err != nil {
return "", err
}

var bibFiles []string
for _, doc := range project.Docs {
if doc.Filepath != "" && strings.HasSuffix(doc.Filepath, ".bib") {
bibFiles = append(bibFiles, doc.Lines...)
}
}
bibliography := strings.Join(bibFiles, "\n")

// Get citation keys from LLM
emptyCitation := "none"
message := fmt.Sprintf("Sentence: %s\nBibliography: %s\nBased on the sentence and bibliography, suggest only the most relevant citation keys separated by commas with no spaces (e.g. key1,key2). Be selective and only include citations that are directly relevant. Avoid suggesting more than 3 citations. If no relevant citations are found, return '%s'.", sentence, bibliography, emptyCitation)

_, resp, err := a.ChatCompletionV2(ctx, "gpt-5.2", OpenAIChatHistory{
openai.SystemMessage("You are a helpful assistant that suggests relevant citation keys."),
openai.UserMessage(message),
}, llmProvider)

if err != nil {
return "", err
}

if len(resp) == 0 {
return "", nil
}

citationKeys := strings.TrimSpace(resp[0].Payload.GetAssistant().GetContent())

if citationKeys == emptyCitation {
return "", nil
}

return citationKeys, nil
}
148 changes: 135 additions & 13 deletions pkg/gen/api/chat/v2/chat.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading