Skip to content

Commit e2f18f3

Browse files
authored
Merge pull request #1 from devalexandre/feat/add-simple-run
feat: add simple run
2 parents 18730f8 + 911fb35 commit e2f18f3

3 files changed

Lines changed: 132 additions & 4 deletions

File tree

client_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"os"
78
"testing"
9+
"time"
810

911
"github.com/google/uuid"
1012
"github.com/tmc/langchaingo/llms"
@@ -122,6 +124,7 @@ func TestRun(t *testing.T) {
122124

123125
// use 2 chains
124126
t.Run("use with 2 traces", func(t *testing.T) {
127+
t.Skip()
125128
// Create a new client
126129
runId := uuid.New().String()
127130
client := NewClient("")
@@ -201,4 +204,92 @@ func TestRun(t *testing.T) {
201204

202205
})
203206

207+
t.Run("use with 2 traces and SimpleRun", func(t *testing.T) {
208+
// Create a new client
209+
runId := uuid.New().String()
210+
client := NewClient(os.Getenv("LANGSMITH_API_KEY"))
211+
212+
opts := []openai.Option{
213+
openai.WithModel("gpt-3.5-turbo-0125"),
214+
openai.WithEmbeddingModel("text-embedding-3-large"),
215+
}
216+
llm, err := openai.New(opts...)
217+
if err != nil {
218+
log.Fatal(err)
219+
}
220+
221+
ctx := context.Background()
222+
223+
content := []llms.MessageContent{
224+
llms.TextParts(llms.ChatMessageTypeSystem, "You are a company branding design wizard."),
225+
llms.TextParts(llms.ChatMessageTypeHuman, "What would be a good company name a company that makes colorful socks?"),
226+
}
227+
228+
if err != nil {
229+
t.Errorf("Error running: %v", err)
230+
}
231+
startTime := time.Now().UTC()
232+
out, err := llm.GenerateContent(ctx, content)
233+
if err != nil {
234+
t.Errorf("Error running: %v", err)
235+
236+
}
237+
runPayload := &RunPayload{
238+
Name: "langsmithgo-llm",
239+
SessionName: "langsmithgo",
240+
RunType: LLM,
241+
RunID: runId,
242+
Tags: []string{"llm"},
243+
StartTime: startTime,
244+
Inputs: map[string]interface{}{
245+
"prompt": content, // Ensure 'output' is properly defined and is of type that has a String method
246+
"model": "gpt-3.5-turbo",
247+
"temperature": 0.7, // Assuming 'temperature' should be a float, not a string
248+
},
249+
Outputs: map[string]interface{}{
250+
"output": out,
251+
},
252+
EndTime: time.Now().UTC(),
253+
}
254+
255+
err = client.RunSingle(runPayload)
256+
257+
if err != nil {
258+
t.Errorf("Error running: %v", err)
259+
}
260+
261+
embdId := uuid.New().String()
262+
// create embedding
263+
264+
startTime = time.Now().UTC()
265+
266+
embedings, err := llm.CreateEmbedding(ctx, []string{"ola", "mundo"})
267+
if err != nil {
268+
log.Fatal(err)
269+
}
270+
271+
err = client.RunSingle(&RunPayload{
272+
Name: "langsmithgo-llm",
273+
SessionName: "langsmithgo",
274+
RunType: Embedding,
275+
StartTime: startTime,
276+
RunID: embdId,
277+
ParentID: runId,
278+
Tags: []string{"llm"},
279+
Inputs: map[string]interface{}{
280+
"prompt": out.Choices[0].Content, // Ensure 'output' is properly defined and is of type that has a String method
281+
"model": "gpt-3.5-turbo",
282+
"temperature": 0.7, // Assuming 'temperature' should be a float, not a string
283+
},
284+
Outputs: map[string]interface{}{
285+
"output": embedings,
286+
},
287+
EndTime: time.Now().UTC(),
288+
})
289+
290+
if err != nil {
291+
log.Fatal(err)
292+
}
293+
294+
})
204295
}

cliente.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func (c *Client) PostRun(input *RunPayload) error {
3434
Tags: input.Tags,
3535
ParentId: input.ParentID,
3636
Extras: input.Extras,
37-
Metadata: input.Metadata,
3837
}
3938

4039
jsonData, err := json.Marshal(payload)
@@ -77,6 +76,41 @@ func (c *Client) Run(input *RunPayload) error {
7776

7877
}
7978

79+
/*
80+
Single Requests
81+
If you want to reduce the number of requests you make to LangSmith, you can log runs in a single request. Just be sure to include the outputs or error and fix the end_time all in the post request.
82+
Below is an example that logs the completion LLM run from above in a single call.
83+
*/
84+
85+
func (c *Client) RunSingle(input *RunPayload) error {
86+
payload := SimplePayload{
87+
PostPayload: PostPayload{ // Especificando a struct PostPayload
88+
ID: input.RunID,
89+
Name: input.Name,
90+
RunType: input.RunType,
91+
StartTime: input.StartTime,
92+
Inputs: input.Inputs,
93+
SessionName: input.SessionName,
94+
Tags: input.Tags,
95+
ParentId: input.ParentID,
96+
Extras: input.Extras,
97+
},
98+
PatchPayload: PatchPayload{ // Especificando a struct PatchPayload
99+
Outputs: input.Outputs,
100+
EndTime: input.EndTime,
101+
Events: input.Events,
102+
},
103+
}
104+
105+
jsonData, err := json.Marshal(payload)
106+
if err != nil {
107+
return err
108+
}
109+
err = c.Do(BASE_URL, http.MethodPost, jsonData)
110+
111+
return err
112+
}
113+
80114
// client for http requests
81115
func (c *Client) Do(url string, method string, jsonData []byte) error {
82116
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))

contracts.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ type RunPayload struct {
3030
Outputs map[string]interface{} `json:"outputs"`
3131
EndTime time.Time `json:"end_time"`
3232
Extras map[string]interface{} `json:"extras"`
33-
Metadata map[string]interface{} `json:"metadata"`
3433
Events []Event `json:"events"`
3534
}
3635

3736
type Client struct {
3837
APIKey string
3938
}
4039

40+
type SimplePayload struct {
41+
PostPayload
42+
PatchPayload
43+
}
44+
4145
type PostPayload struct {
4246
ID string `json:"id"`
4347
Name string `json:"name"`
@@ -47,8 +51,7 @@ type PostPayload struct {
4751
SessionName string `json:"session_name"`
4852
Tags []string `json:"tags,omitempty"`
4953
ParentId string `json:"parent_run_id,omitempty"`
50-
Extras map[string]interface{} `json:"extras,omitempty",`
51-
Metadata map[string]interface{} `json:"metadata,omitempty"`
54+
Extras map[string]interface{} `json:"extras,omitempty"`
5255
}
5356

5457
type PatchPayload struct {

0 commit comments

Comments
 (0)