-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (136 loc) · 3.6 KB
/
main.go
File metadata and controls
166 lines (136 loc) · 3.6 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"fmt"
"strings"
"sync"
"time"
codexsdk "github.com/ethpandaops/codex-agent-sdk-go"
"golang.org/x/sync/errgroup"
)
// translationResult holds a single translation attempt.
type translationResult struct {
Style string
Text string
Cost float64
}
// getAssistantText extracts text content and cost from the message stream.
func getAssistantText(
ctx context.Context,
msgs func(func(codexsdk.Message, error) bool),
) (string, float64, error) {
var (
text string
cost float64
)
for msg, err := range msgs {
if err != nil {
return "", 0, fmt.Errorf("query: %w", err)
}
if m, ok := msg.(*codexsdk.AssistantMessage); ok {
for _, block := range m.Content {
if tb, ok := block.(*codexsdk.TextBlock); ok {
text = tb.Text
}
}
}
if m, ok := msg.(*codexsdk.ResultMessage); ok {
if m.Usage != nil {
cost = float64(m.Usage.InputTokens + m.Usage.OutputTokens)
}
}
}
return text, cost, nil
}
func parallelTranslations() {
fmt.Println("=== Parallel Translations ===")
fmt.Println("Running 3 translation styles concurrently, then picking the best.")
fmt.Println()
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
originalText := "To be, or not to be, that is the question."
styles := []struct {
Name string
Prompt string
}{
{
Name: "Formal",
Prompt: "You are a formal translator. Translate to French using literary, formal language.",
},
{
Name: "Casual",
Prompt: "You are a casual translator. Translate to French using everyday, colloquial language.",
},
{
Name: "Poetic",
Prompt: "You are a poetic translator. Translate to French preserving rhythm and beauty.",
},
}
var (
mu sync.Mutex
results = make([]translationResult, 0, len(styles))
)
g, gCtx := errgroup.WithContext(ctx)
for _, style := range styles {
g.Go(func() error {
text, cost, err := getAssistantText(gCtx, codexsdk.Query(gCtx,
codexsdk.Text(fmt.Sprintf("Translate this to French: %q", originalText)),
codexsdk.WithSystemPrompt(style.Prompt),
))
if err != nil {
return fmt.Errorf("%s translation: %w", style.Name, err)
}
mu.Lock()
results = append(results, translationResult{
Style: style.Name,
Text: text,
Cost: cost,
})
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Display all translations
var (
totalCost float64
descriptions []string
)
for _, r := range results {
fmt.Printf("[%s] %s\n", r.Style, r.Text)
totalCost += r.Cost
descriptions = append(descriptions,
fmt.Sprintf("- %s: %s", r.Style, r.Text))
}
fmt.Printf("\nTranslation cost: $%.4f\n", totalCost)
// Use a judge query to pick the best
fmt.Println("\n--- Judge Evaluation ---")
judgePrompt := fmt.Sprintf(
"Original: %q\n\nTranslations:\n%s\n\n"+
"Which translation is best and why? Be concise (2-3 sentences).",
originalText, strings.Join(descriptions, "\n"),
)
judgeText, judgeCost, err := getAssistantText(ctx, codexsdk.Query(ctx,
codexsdk.Text(judgePrompt),
codexsdk.WithSystemPrompt(
"You are a French language expert. Evaluate translations for accuracy and style.",
),
))
if err != nil {
fmt.Printf("Judge error: %v\n", err)
return
}
fmt.Printf("Judge: %s\n", judgeText)
fmt.Printf("\nTotal cost (translations + judge): $%.4f\n", totalCost+judgeCost)
fmt.Println()
}
func main() {
fmt.Println("Parallel Queries Examples")
fmt.Println()
fmt.Println("Demonstrates running multiple Query() calls concurrently with errgroup.")
fmt.Println()
parallelTranslations()
}