-
Notifications
You must be signed in to change notification settings - Fork 12
feat(logs): Add tool call, agent logs #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7a44a86
added function
nina-kollman cb234b1
added example
nina-kollman ad406ac
split task
nina-kollman 37ff07d
divide
nina-kollman 6cfec4e
wip
nina-kollman eba85ce
add end
nina-kollman aa91b3e
works
nina-kollman eb0a973
lint
nina-kollman 735f968
build agent
nina-kollman 3f52b3f
added context
nina-kollman 01c1198
add prop
nina-kollman 9cf4d61
works
nina-kollman 95f583d
move to att
nina-kollman 5d3cf19
comments
nina-kollman e32cc3f
typo
nina-kollman 2e9a52c
comm
nina-kollman 6f6e7c0
semiconv
nina-kollman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ | |
| *.so | ||
| *.tar.gz | ||
| /release/ | ||
| sample-app | ||
|
|
||
| # Test binary, built with `go test -c` | ||
| *.test | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/sashabaranov/go-openai" | ||
| sdk "github.com/traceloop/go-openllmetry/traceloop-sdk" | ||
| ) | ||
|
|
||
| func createJoke(ctx context.Context, workflow *sdk.Workflow, client *openai.Client) (string, error) { | ||
| task := workflow.NewTask("joke_creation") | ||
| defer task.End() | ||
|
|
||
| // Log prompt | ||
| prompt := sdk.Prompt{ | ||
| Vendor: "openai", | ||
| Mode: "chat", | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []sdk.Message{ | ||
| { | ||
| Index: 0, | ||
| Role: "user", | ||
| Content: "Tell me a joke about opentelemetry", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| llmSpan := task.LogPrompt(prompt) | ||
|
|
||
| // Make API call | ||
| resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []openai.ChatCompletionMessage{ | ||
| { | ||
| Role: "user", | ||
| Content: "Tell me a joke about opentelemetry", | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("CreateChatCompletion error: %w", err) | ||
| } | ||
|
|
||
| // Log completion | ||
| var completionMsgs []sdk.Message | ||
| for _, choice := range resp.Choices { | ||
| completionMsgs = append(completionMsgs, sdk.Message{ | ||
| Index: choice.Index, | ||
| Content: choice.Message.Content, | ||
| Role: choice.Message.Role, | ||
| }) | ||
| } | ||
|
|
||
| llmSpan.LogCompletion(ctx, sdk.Completion{ | ||
| Model: resp.Model, | ||
| Messages: completionMsgs, | ||
| }, sdk.Usage{ | ||
| TotalTokens: resp.Usage.TotalTokens, | ||
| CompletionTokens: resp.Usage.CompletionTokens, | ||
| PromptTokens: resp.Usage.PromptTokens, | ||
| }) | ||
|
|
||
| return resp.Choices[0].Message.Content, nil | ||
| } | ||
|
|
||
| func translateJokeToPirate(ctx context.Context, workflow *sdk.Workflow, client *openai.Client, joke string) (string, error) { | ||
| // Log prompt | ||
| piratePrompt := fmt.Sprintf("Translate the below joke to pirate-like english:\n\n%s", joke) | ||
| prompt := sdk.Prompt{ | ||
| Vendor: "openai", | ||
| Mode: "chat", | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []sdk.Message{ | ||
| { | ||
| Index: 0, | ||
| Role: "user", | ||
| Content: piratePrompt, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| agent := workflow.NewAgent("joke_translation", map[string]string{ | ||
| "translation_type": "pirate", | ||
| }) | ||
| defer agent.End() | ||
|
|
||
| llmSpan := agent.LogPrompt(prompt) | ||
|
|
||
| // Make API call | ||
| resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []openai.ChatCompletionMessage{ | ||
| { | ||
| Role: "user", | ||
| Content: piratePrompt, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("CreateChatCompletion error: %w", err) | ||
| } | ||
|
|
||
| // Log completion | ||
| var completionMsgs []sdk.Message | ||
| for _, choice := range resp.Choices { | ||
| completionMsgs = append(completionMsgs, sdk.Message{ | ||
| Index: choice.Index, | ||
| Content: choice.Message.Content, | ||
| Role: choice.Message.Role, | ||
| }) | ||
| } | ||
|
|
||
| llmSpan.LogCompletion(ctx, sdk.Completion{ | ||
| Model: resp.Model, | ||
| Messages: completionMsgs, | ||
| }, sdk.Usage{ | ||
| TotalTokens: resp.Usage.TotalTokens, | ||
| CompletionTokens: resp.Usage.CompletionTokens, | ||
| PromptTokens: resp.Usage.PromptTokens, | ||
| }) | ||
|
|
||
| // Call history jokes tool | ||
| _, err = historyJokesTool(ctx, agent, client) | ||
| if err != nil { | ||
| fmt.Printf("Warning: history_jokes_tool error: %v\n", err) | ||
| } | ||
|
|
||
| return resp.Choices[0].Message.Content, nil | ||
nina-kollman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func historyJokesTool(ctx context.Context, agent *sdk.Agent, client *openai.Client) (string, error) { | ||
| // Log prompt | ||
| prompt := sdk.Prompt{ | ||
| Vendor: "openai", | ||
| Mode: "chat", | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []sdk.Message{ | ||
| { | ||
| Index: 0, | ||
| Role: "user", | ||
| Content: "get some history jokes", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| tool := agent.NewTool("history_jokes", "function", sdk.ToolFunction{ | ||
| Name: "history_jokes", | ||
| Description: "Get some history jokes", | ||
| Parameters: map[string]interface{}{}, | ||
| }, map[string]string{ | ||
| "user_id": "user_12345", | ||
| }) | ||
| defer tool.End() | ||
|
|
||
| llmSpan := tool.LogPrompt(prompt) | ||
|
|
||
| // Make API call | ||
| resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []openai.ChatCompletionMessage{ | ||
| { | ||
| Role: "user", | ||
| Content: "get some history jokes", | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("CreateChatCompletion error: %w", err) | ||
| } | ||
|
|
||
| // Log completion | ||
| var completionMsgs []sdk.Message | ||
| for _, choice := range resp.Choices { | ||
| completionMsgs = append(completionMsgs, sdk.Message{ | ||
| Index: choice.Index, | ||
| Content: choice.Message.Content, | ||
| Role: choice.Message.Role, | ||
| }) | ||
| } | ||
|
|
||
| llmSpan.LogCompletion(ctx, sdk.Completion{ | ||
| Model: resp.Model, | ||
| Messages: completionMsgs, | ||
| }, sdk.Usage{ | ||
| TotalTokens: resp.Usage.TotalTokens, | ||
| CompletionTokens: resp.Usage.CompletionTokens, | ||
| PromptTokens: resp.Usage.PromptTokens, | ||
| }) | ||
|
|
||
| return resp.Choices[0].Message.Content, nil | ||
nina-kollman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func generateSignature(ctx context.Context, workflow *sdk.Workflow, client *openai.Client, joke string) (string, error) { | ||
| task := workflow.NewTask("signature_generation") | ||
| defer task.End() | ||
|
|
||
| signaturePrompt := "add a signature to the joke:\n\n" + joke | ||
|
|
||
| // Log prompt | ||
| prompt := sdk.Prompt{ | ||
| Vendor: "openai", | ||
| Mode: "completion", | ||
| Model: "davinci-002", | ||
| Messages: []sdk.Message{ | ||
| { | ||
| Index: 0, | ||
| Role: "user", | ||
| Content: signaturePrompt, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| llmSpan := task.LogPrompt(prompt) | ||
|
|
||
| // Make API call | ||
| resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{ | ||
| Model: "davinci-002", | ||
| Prompt: signaturePrompt, | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("CreateCompletion error: %w", err) | ||
| } | ||
|
|
||
| // Log completion | ||
| llmSpan.LogCompletion(ctx, sdk.Completion{ | ||
| Model: resp.Model, | ||
| Messages: []sdk.Message{ | ||
| { | ||
| Index: 0, | ||
| Role: "assistant", | ||
| Content: resp.Choices[0].Text, | ||
| }, | ||
| }, | ||
| }, sdk.Usage{ | ||
| TotalTokens: resp.Usage.TotalTokens, | ||
| CompletionTokens: resp.Usage.CompletionTokens, | ||
| PromptTokens: resp.Usage.PromptTokens, | ||
| }) | ||
|
|
||
| return resp.Choices[0].Text, nil | ||
nina-kollman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func runJokeWorkflow() { | ||
| ctx := context.Background() | ||
|
|
||
| // Initialize Traceloop SDK | ||
| traceloop, err := sdk.NewClient(ctx, sdk.Config{ | ||
| APIKey: os.Getenv("TRACELOOP_API_KEY"), | ||
| }) | ||
| if err != nil { | ||
| fmt.Printf("NewClient error: %v\n", err) | ||
| return | ||
| } | ||
| defer func() { traceloop.Shutdown(ctx) }() | ||
|
|
||
| client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) | ||
|
|
||
| // Create workflow | ||
| wf := traceloop.NewWorkflow(ctx, sdk.WorkflowAttributes{ | ||
| Name: "go-joke_generator", | ||
| AssociationProperties: map[string]string{ | ||
| "user_id": "user_12345", | ||
| "chat_id": "chat_1234", | ||
| }, | ||
| }) | ||
| defer wf.End() | ||
|
|
||
| // Execute workflow steps | ||
| fmt.Println("Creating joke...") | ||
| engJoke, err := createJoke(ctx, wf, client) | ||
| if err != nil { | ||
| fmt.Printf("Error creating joke: %v\n", err) | ||
| return | ||
| } | ||
| fmt.Printf("\nEnglish joke:\n%s\n\n", engJoke) | ||
|
|
||
| fmt.Println("Translating to pirate...") | ||
| pirateJoke, err := translateJokeToPirate(ctx, wf, client, engJoke) | ||
| if err != nil { | ||
| fmt.Printf("Error translating joke: %v\n", err) | ||
| return | ||
| } | ||
| fmt.Printf("\nPirate joke:\n%s\n\n", pirateJoke) | ||
|
|
||
| fmt.Println("Generating signature...") | ||
| signature, err := generateSignature(ctx, wf, client, pirateJoke) | ||
| if err != nil { | ||
| fmt.Printf("Error generating signature: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| // Combine result | ||
| result := pirateJoke + "\n\n" + signature | ||
| fmt.Printf("\n=== Final Result ===\n%s\n", result) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.