-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqa_processor.go
135 lines (110 loc) · 3.66 KB
/
qa_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"fmt"
"strings"
"github.com/XiaoConstantine/dspy-go/pkg/agents"
"github.com/XiaoConstantine/dspy-go/pkg/core"
"github.com/XiaoConstantine/dspy-go/pkg/modules"
)
type RepoQAProcessor struct {
ragStore RAGStore
}
type QAResponse struct {
Answer string `json:"answer"` // The detailed answer to the question
Confidence float64 `json:"confidence"` // How confident the system is about the answer (0.0-1.0)
SourceFiles []string `json:"source_files"` // Files referenced in the answer
}
func NewRepoQAProcessor(store RAGStore) *RepoQAProcessor {
return &RepoQAProcessor{
ragStore: store,
}
}
// Process implements the TaskProcessor interface.
func (p *RepoQAProcessor) Process(ctx context.Context, task agents.Task, context map[string]interface{}) (interface{}, error) {
signature := core.NewSignature(
[]core.InputField{
{Field: core.Field{Name: "question"}},
{Field: core.Field{Name: "relevant_context"}},
},
[]core.OutputField{
{Field: core.NewField("answer")},
{Field: core.NewField("confidence")},
{Field: core.NewField("source_files")},
},
).WithInstruction(`Answer questions about the repository using the provided context.
Follow repository conventions and patterns when explaining code.
Reference specific files and line numbers when available.`)
metadata, err := extractQAMetadata(task.Metadata)
if err != nil {
return nil, fmt.Errorf("task %s: %w", task.ID, err)
}
// Create embedding and find similar content
llm := core.GetTeacherLLM()
questionEmbedding, err := llm.CreateEmbedding(ctx, metadata.Question)
if err != nil {
return nil, fmt.Errorf("failed to create embedding: %w", err)
}
similar, err := p.ragStore.FindSimilar(ctx, questionEmbedding.Vector, 10)
if err != nil {
return nil, fmt.Errorf("failed to find similar content: %w", err)
}
// Format context for LLM
contextBuilder := strings.Builder{}
sourceFiles := make([]string, 0, len(similar))
for _, content := range similar {
contextBuilder.WriteString(fmt.Sprintf("File: %s\n", content.Metadata["file_path"]))
contextBuilder.WriteString(fmt.Sprintf("Lines %s-%s:\n",
content.Metadata["start_line"],
content.Metadata["end_line"]))
contextBuilder.WriteString(content.Text)
contextBuilder.WriteString("\n---\n")
sourceFiles = append(sourceFiles, content.Metadata["file_path"])
}
// Use predict module like other processors
predict := modules.NewPredict(signature)
result, err := predict.Process(ctx, map[string]interface{}{
"question": metadata.Question,
"relevant_context": contextBuilder.String(),
})
if err != nil {
return nil, fmt.Errorf("prediction failed: %w", err)
}
response := &QAResponse{
SourceFiles: sourceFiles,
}
if err := extractQAResult(result, response); err != nil {
return nil, fmt.Errorf("failed to extract response: %w", err)
}
return response, nil
}
// Helper structs and functions.
type QAMetadata struct {
Question string
}
func extractQAMetadata(metadata map[string]interface{}) (*QAMetadata, error) {
question, ok := metadata["question"].(string)
if !ok {
return nil, fmt.Errorf("missing or invalid question in metadata")
}
return &QAMetadata{
Question: question,
}, nil
}
func extractQAResult(result interface{}, response *QAResponse) error {
resultMap, ok := result.(map[string]interface{})
if !ok {
return fmt.Errorf("invalid result type: %T", result)
}
if answer, ok := resultMap["answer"].(string); ok {
response.Answer = answer
} else {
return fmt.Errorf("missing or invalid answer")
}
if confidence, ok := resultMap["confidence"].(float64); ok {
response.Confidence = confidence
} else {
response.Confidence = 0.7
}
return nil
}