|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/sashabaranov/go-openai" |
| 10 | + tlp "github.com/traceloop/go-openllmetry/traceloop-sdk" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + ctx := context.Background() |
| 15 | + |
| 16 | + traceloop, err := tlp.NewClient(ctx, tlp.Config{ |
| 17 | + BaseURL: "api-staging.traceloop.com", |
| 18 | + APIKey: os.Getenv("TRACELOOP_API_KEY"), |
| 19 | + }) |
| 20 | + defer func() { traceloop.Shutdown(ctx) }() |
| 21 | + |
| 22 | + if err != nil { |
| 23 | + fmt.Printf("NewClient error: %v\n", err) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + wf := traceloop.NewWorkflow(ctx, tlp.WorkflowAttributes{ |
| 28 | + Name: "history_generation", |
| 29 | + }) |
| 30 | + defer wf.End() |
| 31 | + |
| 32 | + factGenTask := wf.NewTask("current_date_fact_generation") |
| 33 | + defer factGenTask.End() |
| 34 | + |
| 35 | + request, err := traceloop.GetOpenAIChatCompletionRequest("example-prompt", map[string]interface{}{ "date": time.Now().Format("01/02") }) |
| 36 | + if err != nil { |
| 37 | + fmt.Printf("GetOpenAIChatCompletionRequest error: %v\n", err) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + var promptMsgs []tlp.Message |
| 42 | + for i, message := range request.Messages { |
| 43 | + promptMsgs = append(promptMsgs, tlp.Message{ |
| 44 | + Index: i, |
| 45 | + Content: message.Content, |
| 46 | + Role: message.Role, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + llmSpan, err := factGenTask.LogPrompt( |
| 51 | + tlp.Prompt{ |
| 52 | + Vendor: "openai", |
| 53 | + Mode: "chat", |
| 54 | + Model: request.Model, |
| 55 | + Messages: promptMsgs, |
| 56 | + }, |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + fmt.Printf("LogPrompt error: %v\n", err) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) |
| 64 | + resp, err := client.CreateChatCompletion( |
| 65 | + context.Background(), |
| 66 | + *request, |
| 67 | + ) |
| 68 | + if err != nil { |
| 69 | + fmt.Printf("ChatCompletion error: %v\n", err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + var completionMsgs []tlp.Message |
| 74 | + for _, choice := range resp.Choices { |
| 75 | + completionMsgs = append(completionMsgs, tlp.Message{ |
| 76 | + Index: choice.Index, |
| 77 | + Content: choice.Message.Content, |
| 78 | + Role: choice.Message.Role, |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + llmSpan.LogCompletion(ctx, tlp.Completion{ |
| 83 | + Model: resp.Model, |
| 84 | + Messages: completionMsgs, |
| 85 | + }, tlp.Usage{ |
| 86 | + TotalTokens: resp.Usage.TotalTokens, |
| 87 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 88 | + PromptTokens: resp.Usage.PromptTokens, |
| 89 | + }) |
| 90 | + |
| 91 | + someOtherTask := wf.NewTask("some_other_task") |
| 92 | + defer someOtherTask.End() |
| 93 | + |
| 94 | + otherPrompt, _ := someOtherTask.LogPrompt(tlp.Prompt{ |
| 95 | + Vendor: "openai", |
| 96 | + Mode: "chat", |
| 97 | + Model: request.Model, |
| 98 | + Messages: []tlp.Message{ |
| 99 | + { |
| 100 | + Index: 0, |
| 101 | + Content: "some other prompt", |
| 102 | + Role: "user", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + otherPrompt.LogCompletion(ctx, tlp.Completion{ |
| 108 | + Model: resp.Model, |
| 109 | + Messages: completionMsgs, |
| 110 | + }, tlp.Usage{ |
| 111 | + TotalTokens: resp.Usage.TotalTokens, |
| 112 | + CompletionTokens: resp.Usage.CompletionTokens, |
| 113 | + PromptTokens: resp.Usage.PromptTokens, |
| 114 | + }) |
| 115 | + |
| 116 | + fmt.Println(resp.Choices[0].Message.Content) |
| 117 | +} |
0 commit comments