|
| 1 | +package langsmithgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/tmc/langchaingo/llms" |
| 11 | + "github.com/tmc/langchaingo/llms/openai" |
| 12 | +) |
| 13 | + |
| 14 | +func TestMain(m *testing.M) { |
| 15 | + m.Run() |
| 16 | + |
| 17 | +} |
| 18 | +func TestRun(t *testing.T) { |
| 19 | + t.Run("use with GenerateFromSinglePrompt", func(t *testing.T) { |
| 20 | + t.Skip() |
| 21 | + // Create a new client |
| 22 | + runId := uuid.New().String() |
| 23 | + log.Println("runId: ", runId) |
| 24 | + client := NewClient("") |
| 25 | + prompt := "The first man to walk on the moon" |
| 26 | + llm, err := openai.New() |
| 27 | + if err != nil { |
| 28 | + t.Errorf("Error creating LLM: %v", err) |
| 29 | + } |
| 30 | + ctx := context.Background() |
| 31 | + err = client.Run(&RunPayload{ |
| 32 | + Name: "langsmithgo-chain", |
| 33 | + SessionName: "langsmithgo", |
| 34 | + RunType: Chain, |
| 35 | + RunID: runId, |
| 36 | + Inputs: map[string]interface{}{ |
| 37 | + "prompt": prompt, |
| 38 | + }, |
| 39 | + }) |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + t.Errorf("Error running: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + completion, err := llms.GenerateFromSinglePrompt(ctx, |
| 46 | + llm, |
| 47 | + prompt, |
| 48 | + llms.WithTemperature(0.8), |
| 49 | + llms.WithStopWords([]string{"Armstrong"}), |
| 50 | + ) |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("error generating completion: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + err = client.Run(&RunPayload{ |
| 56 | + RunID: runId, |
| 57 | + Outputs: map[string]interface{}{ |
| 58 | + "output": completion, |
| 59 | + }, |
| 60 | + }) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + t.Errorf("Error running: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Println(completion) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("use with Chain", func(t *testing.T) { |
| 70 | + t.Skip() |
| 71 | + // Create a new client |
| 72 | + runId := uuid.New().String() |
| 73 | + client := NewClient("") |
| 74 | + |
| 75 | + opts := []openai.Option{ |
| 76 | + openai.WithModel("gpt-3.5-turbo"), |
| 77 | + } |
| 78 | + llm, err := openai.New(opts...) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("Error creating LLM: %v", err) |
| 81 | + } |
| 82 | + ctx := context.Background() |
| 83 | + |
| 84 | + content := []llms.MessageContent{ |
| 85 | + llms.TextParts(llms.ChatMessageTypeSystem, "You are a company branding design wizard."), |
| 86 | + llms.TextParts(llms.ChatMessageTypeHuman, "What would be a good company name a company that makes colorful socks?"), |
| 87 | + } |
| 88 | + |
| 89 | + err = client.Run(&RunPayload{ |
| 90 | + Name: "langsmithgo-llm", |
| 91 | + SessionName: "langsmithgo", |
| 92 | + RunType: LLM, |
| 93 | + RunID: runId, |
| 94 | + Tags: []string{"llm"}, |
| 95 | + Inputs: map[string]interface{}{ |
| 96 | + "prompt": content, // Ensure 'output' is properly defined and is of type that has a String method |
| 97 | + "model": "gpt-3.5-turbo", |
| 98 | + "temperature": 0.7, // Assuming 'temperature' should be a float, not a string |
| 99 | + }, |
| 100 | + }) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + t.Errorf("Error running: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + out, err := llm.GenerateContent(ctx, content) |
| 107 | + if err != nil { |
| 108 | + t.Errorf("Error running: %v", err) |
| 109 | + } |
| 110 | + err = client.Run(&RunPayload{ |
| 111 | + ParentID: runId, |
| 112 | + Outputs: map[string]interface{}{ |
| 113 | + "output": out, |
| 114 | + }, |
| 115 | + }) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + t.Errorf("Error running: %v", err) |
| 119 | + } |
| 120 | + |
| 121 | + }) |
| 122 | + |
| 123 | + // use 2 chains |
| 124 | + t.Run("use with 2 traces", func(t *testing.T) { |
| 125 | + // Create a new client |
| 126 | + runId := uuid.New().String() |
| 127 | + client := NewClient("") |
| 128 | + |
| 129 | + opts := []openai.Option{ |
| 130 | + openai.WithModel("gpt-3.5-turbo-0125"), |
| 131 | + openai.WithEmbeddingModel("text-embedding-3-large"), |
| 132 | + } |
| 133 | + llm, err := openai.New(opts...) |
| 134 | + if err != nil { |
| 135 | + log.Fatal(err) |
| 136 | + } |
| 137 | + |
| 138 | + ctx := context.Background() |
| 139 | + |
| 140 | + content := []llms.MessageContent{ |
| 141 | + llms.TextParts(llms.ChatMessageTypeSystem, "You are a company branding design wizard."), |
| 142 | + llms.TextParts(llms.ChatMessageTypeHuman, "What would be a good company name a company that makes colorful socks?"), |
| 143 | + } |
| 144 | + |
| 145 | + err = client.Run(&RunPayload{ |
| 146 | + Name: "langsmithgo-llm", |
| 147 | + SessionName: "langsmithgo", |
| 148 | + RunType: LLM, |
| 149 | + RunID: runId, |
| 150 | + Tags: []string{"llm"}, |
| 151 | + Inputs: map[string]interface{}{ |
| 152 | + "prompt": content, // Ensure 'output' is properly defined and is of type that has a String method |
| 153 | + "model": "gpt-3.5-turbo", |
| 154 | + "temperature": 0.7, // Assuming 'temperature' should be a float, not a string |
| 155 | + }, |
| 156 | + }) |
| 157 | + |
| 158 | + if err != nil { |
| 159 | + t.Errorf("Error running: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + out, err := llm.GenerateContent(ctx, content) |
| 163 | + if err != nil { |
| 164 | + t.Errorf("Error running: %v", err) |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + err = client.Run(&RunPayload{ |
| 169 | + RunID: runId, |
| 170 | + Outputs: map[string]interface{}{ |
| 171 | + "output": out, |
| 172 | + }, |
| 173 | + }) |
| 174 | + |
| 175 | + embdId := uuid.New().String() |
| 176 | + // create embedding |
| 177 | + err = client.Run(&RunPayload{ |
| 178 | + Name: "langsmithgo-llm", |
| 179 | + SessionName: "langsmithgo", |
| 180 | + RunType: Embedding, |
| 181 | + RunID: embdId, |
| 182 | + ParentID: runId, |
| 183 | + Tags: []string{"llm"}, |
| 184 | + Inputs: map[string]interface{}{ |
| 185 | + "prompt": out.Choices[0].Content, // Ensure 'output' is properly defined and is of type that has a String method |
| 186 | + "model": "gpt-3.5-turbo", |
| 187 | + "temperature": 0.7, // Assuming 'temperature' should be a float, not a string |
| 188 | + }, |
| 189 | + }) |
| 190 | + embedings, err := llm.CreateEmbedding(ctx, []string{"ola", "mundo"}) |
| 191 | + if err != nil { |
| 192 | + log.Fatal(err) |
| 193 | + } |
| 194 | + |
| 195 | + err = client.Run(&RunPayload{ |
| 196 | + RunID: embdId, |
| 197 | + Outputs: map[string]interface{}{ |
| 198 | + "output": embedings, |
| 199 | + }, |
| 200 | + }) |
| 201 | + |
| 202 | + }) |
| 203 | + |
| 204 | +} |
0 commit comments