Skip to content

Commit 2b08876

Browse files
committed
feat: add run
1 parent 911fb35 commit 2b08876

3 files changed

Lines changed: 45 additions & 29 deletions

File tree

client_test.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/google/uuid"
1211
"github.com/tmc/langchaingo/llms"
1312
"github.com/tmc/langchaingo/llms/openai"
1413
)
1514

1615
func TestMain(m *testing.M) {
16+
1717
m.Run()
1818

1919
}
2020
func TestRun(t *testing.T) {
2121
t.Run("use with GenerateFromSinglePrompt", func(t *testing.T) {
22-
t.Skip()
22+
2323
// Create a new client
24-
runId := uuid.New().String()
25-
log.Println("runId: ", runId)
2624
client := NewClient("")
2725
prompt := "The first man to walk on the moon"
2826
llm, err := openai.New()
@@ -34,7 +32,6 @@ func TestRun(t *testing.T) {
3432
Name: "langsmithgo-chain",
3533
SessionName: "langsmithgo",
3634
RunType: Chain,
37-
RunID: runId,
3835
Inputs: map[string]interface{}{
3936
"prompt": prompt,
4037
},
@@ -55,7 +52,6 @@ func TestRun(t *testing.T) {
5552
}
5653

5754
err = client.Run(&RunPayload{
58-
RunID: runId,
5955
Outputs: map[string]interface{}{
6056
"output": completion,
6157
},
@@ -69,9 +65,8 @@ func TestRun(t *testing.T) {
6965
})
7066

7167
t.Run("use with Chain", func(t *testing.T) {
72-
t.Skip()
68+
7369
// Create a new client
74-
runId := uuid.New().String()
7570
client := NewClient("")
7671

7772
opts := []openai.Option{
@@ -92,7 +87,6 @@ func TestRun(t *testing.T) {
9287
Name: "langsmithgo-llm",
9388
SessionName: "langsmithgo",
9489
RunType: LLM,
95-
RunID: runId,
9690
Tags: []string{"llm"},
9791
Inputs: map[string]interface{}{
9892
"prompt": content, // Ensure 'output' is properly defined and is of type that has a String method
@@ -110,7 +104,6 @@ func TestRun(t *testing.T) {
110104
t.Errorf("Error running: %v", err)
111105
}
112106
err = client.Run(&RunPayload{
113-
ParentID: runId,
114107
Outputs: map[string]interface{}{
115108
"output": out,
116109
},
@@ -124,9 +117,7 @@ func TestRun(t *testing.T) {
124117

125118
// use 2 chains
126119
t.Run("use with 2 traces", func(t *testing.T) {
127-
t.Skip()
128120
// Create a new client
129-
runId := uuid.New().String()
130121
client := NewClient("")
131122

132123
opts := []openai.Option{
@@ -149,7 +140,6 @@ func TestRun(t *testing.T) {
149140
Name: "langsmithgo-llm",
150141
SessionName: "langsmithgo",
151142
RunType: LLM,
152-
RunID: runId,
153143
Tags: []string{"llm"},
154144
Inputs: map[string]interface{}{
155145
"prompt": content, // Ensure 'output' is properly defined and is of type that has a String method
@@ -169,20 +159,16 @@ func TestRun(t *testing.T) {
169159
}
170160

171161
err = client.Run(&RunPayload{
172-
RunID: runId,
173162
Outputs: map[string]interface{}{
174163
"output": out,
175164
},
176165
})
177166

178-
embdId := uuid.New().String()
179167
// create embedding
180168
err = client.Run(&RunPayload{
181169
Name: "langsmithgo-llm",
182170
SessionName: "langsmithgo",
183171
RunType: Embedding,
184-
RunID: embdId,
185-
ParentID: runId,
186172
Tags: []string{"llm"},
187173
Inputs: map[string]interface{}{
188174
"prompt": out.Choices[0].Content, // Ensure 'output' is properly defined and is of type that has a String method
@@ -196,7 +182,6 @@ func TestRun(t *testing.T) {
196182
}
197183

198184
err = client.Run(&RunPayload{
199-
RunID: embdId,
200185
Outputs: map[string]interface{}{
201186
"output": embedings,
202187
},
@@ -206,9 +191,8 @@ func TestRun(t *testing.T) {
206191

207192
t.Run("use with 2 traces and SimpleRun", func(t *testing.T) {
208193
// Create a new client
209-
runId := uuid.New().String()
210-
client := NewClient(os.Getenv("LANGSMITH_API_KEY"))
211194

195+
client := NewClient(os.Getenv("LANGSMITH_API_KEY"))
212196
opts := []openai.Option{
213197
openai.WithModel("gpt-3.5-turbo-0125"),
214198
openai.WithEmbeddingModel("text-embedding-3-large"),
@@ -238,7 +222,6 @@ func TestRun(t *testing.T) {
238222
Name: "langsmithgo-llm",
239223
SessionName: "langsmithgo",
240224
RunType: LLM,
241-
RunID: runId,
242225
Tags: []string{"llm"},
243226
StartTime: startTime,
244227
Inputs: map[string]interface{}{
@@ -258,9 +241,7 @@ func TestRun(t *testing.T) {
258241
t.Errorf("Error running: %v", err)
259242
}
260243

261-
embdId := uuid.New().String()
262244
// create embedding
263-
264245
startTime = time.Now().UTC()
265246

266247
embedings, err := llm.CreateEmbedding(ctx, []string{"ola", "mundo"})
@@ -273,8 +254,6 @@ func TestRun(t *testing.T) {
273254
SessionName: "langsmithgo",
274255
RunType: Embedding,
275256
StartTime: startTime,
276-
RunID: embdId,
277-
ParentID: runId,
278257
Tags: []string{"llm"},
279258
Inputs: map[string]interface{}{
280259
"prompt": out.Choices[0].Content, // Ensure 'output' is properly defined and is of type that has a String method

cliente.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"github.com/google/uuid"
89
"io"
910
"net/http"
1011
"os"
@@ -24,15 +25,17 @@ func NewClient(apiKey string) *Client {
2425

2526
func (c *Client) PostRun(input *RunPayload) error {
2627

28+
SetRunId(uuid.New().String())
29+
2730
payload := PostPayload{
28-
ID: input.RunID,
31+
ID: GetRunId(),
2932
Name: input.Name,
3033
RunType: input.RunType,
3134
StartTime: time.Now().UTC(),
3235
Inputs: input.Inputs,
3336
SessionName: input.SessionName,
3437
Tags: input.Tags,
35-
ParentId: input.ParentID,
38+
ParentId: GetParentId(),
3639
Extras: input.Extras,
3740
}
3841

@@ -42,6 +45,8 @@ func (c *Client) PostRun(input *RunPayload) error {
4245
}
4346
err = c.Do(BASE_URL, http.MethodPost, jsonData)
4447

48+
SetParentId(GetRunId())
49+
4550
return err
4651
}
4752

@@ -83,16 +88,17 @@ Below is an example that logs the completion LLM run from above in a single call
8388
*/
8489

8590
func (c *Client) RunSingle(input *RunPayload) error {
91+
SetRunId(uuid.New().String())
8692
payload := SimplePayload{
8793
PostPayload: PostPayload{ // Especificando a struct PostPayload
88-
ID: input.RunID,
94+
ID: GetRunId(),
8995
Name: input.Name,
9096
RunType: input.RunType,
9197
StartTime: input.StartTime,
9298
Inputs: input.Inputs,
9399
SessionName: input.SessionName,
94100
Tags: input.Tags,
95-
ParentId: input.ParentID,
101+
ParentId: GetParentId(),
96102
Extras: input.Extras,
97103
},
98104
PatchPayload: PatchPayload{ // Especificando a struct PatchPayload
@@ -108,6 +114,7 @@ func (c *Client) RunSingle(input *RunPayload) error {
108114
}
109115
err = c.Do(BASE_URL, http.MethodPost, jsonData)
110116

117+
SetParentId(GetRunId())
111118
return err
112119
}
113120

contracts.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package langsmithgo
22

33
import (
44
"bytes"
5+
"sync/atomic"
56
"time"
67
)
78

89
const (
910
BASE_URL = "https://api.smith.langchain.com/runs"
1011
)
1112

13+
var runID, parentID atomic.Value
14+
1215
type Response struct {
1316
Detail string `json:"detail"`
1417
}
@@ -91,3 +94,30 @@ func (r RunType) MarshalJSON() ([]byte, error) {
9194
buffer.WriteString(`"`)
9295
return buffer.Bytes(), nil
9396
}
97+
98+
// SetRunId sets the run id
99+
func SetRunId(runId string) {
100+
runID.Store(runId)
101+
}
102+
103+
// GetRunId gets the run id
104+
func GetRunId() string {
105+
if runID.Load() == nil {
106+
return ""
107+
108+
}
109+
return runID.Load().(string)
110+
}
111+
112+
// SetParentId sets the parent id
113+
func SetParentId(parentId string) {
114+
parentID.Store(parentId)
115+
}
116+
117+
// GetParentId gets the parent id
118+
func GetParentId() string {
119+
if parentID.Load() == nil {
120+
return ""
121+
}
122+
return parentID.Load().(string)
123+
}

0 commit comments

Comments
 (0)