|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/sashabaranov/go-openai" |
| 9 | + sdk "github.com/traceloop/go-openllmetry/traceloop-sdk" |
| 10 | +) |
| 11 | + |
| 12 | +func createJoke(ctx context.Context, workflow *sdk.Workflow, client *openai.Client) (string, error) { |
| 13 | + task := workflow.NewTask("joke_creation") |
| 14 | + defer task.End() |
| 15 | + |
| 16 | + // Log prompt |
| 17 | + prompt := sdk.Prompt{ |
| 18 | + Vendor: "openai", |
| 19 | + Mode: "chat", |
| 20 | + Model: "gpt-3.5-turbo", |
| 21 | + Messages: []sdk.Message{ |
| 22 | + { |
| 23 | + Index: 0, |
| 24 | + Role: "user", |
| 25 | + Content: "Tell me a joke about opentelemetry", |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + llmSpan, err := task.LogPrompt(prompt) |
| 31 | + if err != nil { |
| 32 | + return "", fmt.Errorf("LogPrompt error: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Make API call |
| 36 | + resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ |
| 37 | + Model: "gpt-3.5-turbo", |
| 38 | + Messages: []openai.ChatCompletionMessage{ |
| 39 | + { |
| 40 | + Role: "user", |
| 41 | + Content: "Tell me a joke about opentelemetry", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return "", fmt.Errorf("CreateChatCompletion error: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Log completion |
| 50 | + var completionMsgs []sdk.Message |
| 51 | + for _, choice := range resp.Choices { |
| 52 | + completionMsgs = append(completionMsgs, sdk.Message{ |
| 53 | + Index: choice.Index, |
| 54 | + Content: choice.Message.Content, |
| 55 | + Role: choice.Message.Role, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + llmSpan.LogCompletion(ctx, sdk.Completion{ |
| 60 | + Model: resp.Model, |
| 61 | + Messages: completionMsgs, |
| 62 | + }, sdk.Usage{ |
| 63 | + TotalTokens: resp.Usage.TotalTokens, |
| 64 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 65 | + PromptTokens: resp.Usage.PromptTokens, |
| 66 | + }) |
| 67 | + |
| 68 | + return resp.Choices[0].Message.Content, nil |
| 69 | +} |
| 70 | + |
| 71 | +func translateJokeToPirate(ctx context.Context, traceloop *sdk.Traceloop, workflow *sdk.Workflow, client *openai.Client, joke string) (string, error) { |
| 72 | + // Log prompt |
| 73 | + piratePrompt := fmt.Sprintf("Translate the below joke to pirate-like english:\n\n%s", joke) |
| 74 | + prompt := sdk.Prompt{ |
| 75 | + Vendor: "openai", |
| 76 | + Mode: "chat", |
| 77 | + Model: "gpt-3.5-turbo", |
| 78 | + Messages: []sdk.Message{ |
| 79 | + { |
| 80 | + Index: 0, |
| 81 | + Role: "user", |
| 82 | + Content: piratePrompt, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + llmSpan, err := traceloop.LogAgent(ctx, sdk.AgentAttributes{ |
| 88 | + Name: "joke_translation", |
| 89 | + }, prompt, workflow.Attributes) |
| 90 | + if err != nil { |
| 91 | + return "", fmt.Errorf("LogPrompt error: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + // Make API call |
| 95 | + resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ |
| 96 | + Model: "gpt-3.5-turbo", |
| 97 | + Messages: []openai.ChatCompletionMessage{ |
| 98 | + { |
| 99 | + Role: "user", |
| 100 | + Content: piratePrompt, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + return "", fmt.Errorf("CreateChatCompletion error: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Log completion |
| 109 | + var completionMsgs []sdk.Message |
| 110 | + for _, choice := range resp.Choices { |
| 111 | + completionMsgs = append(completionMsgs, sdk.Message{ |
| 112 | + Index: choice.Index, |
| 113 | + Content: choice.Message.Content, |
| 114 | + Role: choice.Message.Role, |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + llmSpan.LogCompletion(ctx, sdk.Completion{ |
| 119 | + Model: resp.Model, |
| 120 | + Messages: completionMsgs, |
| 121 | + }, sdk.Usage{ |
| 122 | + TotalTokens: resp.Usage.TotalTokens, |
| 123 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 124 | + PromptTokens: resp.Usage.PromptTokens, |
| 125 | + }) |
| 126 | + |
| 127 | + // Call history jokes tool |
| 128 | + _, err = historyJokesTool(ctx, traceloop, workflow, client) |
| 129 | + if err != nil { |
| 130 | + fmt.Printf("Warning: history_jokes_tool error: %v\n", err) |
| 131 | + } |
| 132 | + |
| 133 | + return resp.Choices[0].Message.Content, nil |
| 134 | +} |
| 135 | + |
| 136 | +func historyJokesTool(ctx context.Context, traceloop *sdk.Traceloop, workflow *sdk.Workflow, client *openai.Client) (string, error) { |
| 137 | + // Log prompt |
| 138 | + prompt := sdk.Prompt{ |
| 139 | + Vendor: "openai", |
| 140 | + Mode: "chat", |
| 141 | + Model: "gpt-3.5-turbo", |
| 142 | + Messages: []sdk.Message{ |
| 143 | + { |
| 144 | + Index: 0, |
| 145 | + Role: "user", |
| 146 | + Content: "get some history jokes", |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + llmSpan, err := traceloop.LogToolCall(ctx, sdk.ToolCallAttributes{ |
| 152 | + Name: "history_jokes", |
| 153 | + }, prompt, workflow.Attributes) |
| 154 | + if err != nil { |
| 155 | + return "", fmt.Errorf("LogPrompt error: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + // Make API call |
| 159 | + resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ |
| 160 | + Model: "gpt-3.5-turbo", |
| 161 | + Messages: []openai.ChatCompletionMessage{ |
| 162 | + { |
| 163 | + Role: "user", |
| 164 | + Content: "get some history jokes", |
| 165 | + }, |
| 166 | + }, |
| 167 | + }) |
| 168 | + if err != nil { |
| 169 | + return "", fmt.Errorf("CreateChatCompletion error: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + // Log completion |
| 173 | + var completionMsgs []sdk.Message |
| 174 | + for _, choice := range resp.Choices { |
| 175 | + completionMsgs = append(completionMsgs, sdk.Message{ |
| 176 | + Index: choice.Index, |
| 177 | + Content: choice.Message.Content, |
| 178 | + Role: choice.Message.Role, |
| 179 | + }) |
| 180 | + } |
| 181 | + |
| 182 | + llmSpan.LogCompletion(ctx, sdk.Completion{ |
| 183 | + Model: resp.Model, |
| 184 | + Messages: completionMsgs, |
| 185 | + }, sdk.Usage{ |
| 186 | + TotalTokens: resp.Usage.TotalTokens, |
| 187 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 188 | + PromptTokens: resp.Usage.PromptTokens, |
| 189 | + }) |
| 190 | + |
| 191 | + return resp.Choices[0].Message.Content, nil |
| 192 | +} |
| 193 | + |
| 194 | +func generateSignature(ctx context.Context, workflow *sdk.Workflow, client *openai.Client, joke string) (string, error) { |
| 195 | + task := workflow.NewTask("signature_generation") |
| 196 | + defer task.End() |
| 197 | + |
| 198 | + signaturePrompt := "add a signature to the joke:\n\n" + joke |
| 199 | + |
| 200 | + // Log prompt |
| 201 | + prompt := sdk.Prompt{ |
| 202 | + Vendor: "openai", |
| 203 | + Mode: "completion", |
| 204 | + Model: "davinci-002", |
| 205 | + Messages: []sdk.Message{ |
| 206 | + { |
| 207 | + Index: 0, |
| 208 | + Role: "user", |
| 209 | + Content: signaturePrompt, |
| 210 | + }, |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + llmSpan, err := task.LogPrompt(prompt) |
| 215 | + if err != nil { |
| 216 | + return "", fmt.Errorf("LogPrompt error: %w", err) |
| 217 | + } |
| 218 | + |
| 219 | + // Make API call |
| 220 | + resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{ |
| 221 | + Model: "davinci-002", |
| 222 | + Prompt: signaturePrompt, |
| 223 | + }) |
| 224 | + if err != nil { |
| 225 | + return "", fmt.Errorf("CreateCompletion error: %w", err) |
| 226 | + } |
| 227 | + |
| 228 | + // Log completion |
| 229 | + llmSpan.LogCompletion(ctx, sdk.Completion{ |
| 230 | + Model: resp.Model, |
| 231 | + Messages: []sdk.Message{ |
| 232 | + { |
| 233 | + Index: 0, |
| 234 | + Role: "assistant", |
| 235 | + Content: resp.Choices[0].Text, |
| 236 | + }, |
| 237 | + }, |
| 238 | + }, sdk.Usage{ |
| 239 | + TotalTokens: resp.Usage.TotalTokens, |
| 240 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 241 | + PromptTokens: resp.Usage.PromptTokens, |
| 242 | + }) |
| 243 | + |
| 244 | + return resp.Choices[0].Text, nil |
| 245 | +} |
| 246 | + |
| 247 | +func runJokeWorkflow() { |
| 248 | + ctx := context.Background() |
| 249 | + |
| 250 | + // Initialize Traceloop SDK |
| 251 | + traceloop, err := sdk.NewClient(ctx, sdk.Config{ |
| 252 | + APIKey: os.Getenv("TRACELOOP_API_KEY"), |
| 253 | + }) |
| 254 | + if err != nil { |
| 255 | + fmt.Printf("NewClient error: %v\n", err) |
| 256 | + return |
| 257 | + } |
| 258 | + defer func() { traceloop.Shutdown(ctx) }() |
| 259 | + |
| 260 | + // Create OpenAI client |
| 261 | + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) |
| 262 | + |
| 263 | + // Create workflow |
| 264 | + wf := traceloop.NewWorkflow(ctx, sdk.WorkflowAttributes{ |
| 265 | + Name: "go-joke_generator", |
| 266 | + AssociationProperties: map[string]string{ |
| 267 | + "user_id": "user_12345", |
| 268 | + "chat_id": "chat_1234", |
| 269 | + }, |
| 270 | + }) |
| 271 | + defer wf.End() |
| 272 | + |
| 273 | + // Execute workflow steps |
| 274 | + fmt.Println("Creating joke...") |
| 275 | + engJoke, err := createJoke(ctx, wf, client) |
| 276 | + if err != nil { |
| 277 | + fmt.Printf("Error creating joke: %v\n", err) |
| 278 | + return |
| 279 | + } |
| 280 | + fmt.Printf("\nEnglish joke:\n%s\n\n", engJoke) |
| 281 | + |
| 282 | + fmt.Println("Translating to pirate...") |
| 283 | + pirateJoke, err := translateJokeToPirate(ctx, traceloop, wf, client, engJoke) |
| 284 | + if err != nil { |
| 285 | + fmt.Printf("Error translating joke: %v\n", err) |
| 286 | + return |
| 287 | + } |
| 288 | + fmt.Printf("\nPirate joke:\n%s\n\n", pirateJoke) |
| 289 | + |
| 290 | + fmt.Println("Generating signature...") |
| 291 | + signature, err := generateSignature(ctx, wf, client, pirateJoke) |
| 292 | + if err != nil { |
| 293 | + fmt.Printf("Error generating signature: %v\n", err) |
| 294 | + return |
| 295 | + } |
| 296 | + |
| 297 | + // Combine result |
| 298 | + result := pirateJoke + "\n\n" + signature |
| 299 | + fmt.Printf("\n=== Final Result ===\n%s\n", result) |
| 300 | +} |
0 commit comments