Skip to content

Commit 18730f8

Browse files
committed
start beta
1 parent 8efe4c7 commit 18730f8

9 files changed

Lines changed: 797 additions & 0 deletions

File tree

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/langsmithgo.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client_test.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
}

cliente.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package langsmithgo
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
"os"
11+
"regexp"
12+
"time"
13+
)
14+
15+
func NewClient(apiKey string) *Client {
16+
apikey := apiKey
17+
if apikey == "" {
18+
apikey = os.Getenv("LANGSMITH_API_KEY")
19+
}
20+
return &Client{
21+
APIKey: apiKey,
22+
}
23+
}
24+
25+
func (c *Client) PostRun(input *RunPayload) error {
26+
27+
payload := PostPayload{
28+
ID: input.RunID,
29+
Name: input.Name,
30+
RunType: input.RunType,
31+
StartTime: time.Now().UTC(),
32+
Inputs: input.Inputs,
33+
SessionName: input.SessionName,
34+
Tags: input.Tags,
35+
ParentId: input.ParentID,
36+
Extras: input.Extras,
37+
Metadata: input.Metadata,
38+
}
39+
40+
jsonData, err := json.Marshal(payload)
41+
if err != nil {
42+
return err
43+
}
44+
err = c.Do(BASE_URL, http.MethodPost, jsonData)
45+
46+
return err
47+
}
48+
49+
func (c *Client) PatchRun(id string, input *RunPayload) error {
50+
payload := PatchPayload{
51+
Outputs: input.Outputs,
52+
EndTime: time.Now().UTC(),
53+
Events: input.Events,
54+
}
55+
jsonData, err := json.Marshal(payload)
56+
if err != nil {
57+
return err
58+
}
59+
err = c.Do(BASE_URL+"/"+id, http.MethodPatch, jsonData)
60+
61+
return err
62+
}
63+
64+
func (c *Client) Run(input *RunPayload) error {
65+
66+
uuidRegex := `^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$`
67+
match, _ := regexp.MatchString(uuidRegex, input.RunID)
68+
if !match {
69+
return fmt.Errorf("invalid UUID format: %s", input.RunID)
70+
}
71+
72+
if input.Outputs != nil {
73+
return c.PatchRun(input.RunID, input)
74+
}
75+
76+
return c.PostRun(input)
77+
78+
}
79+
80+
// client for http requests
81+
func (c *Client) Do(url string, method string, jsonData []byte) error {
82+
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
83+
if err != nil {
84+
panic(err)
85+
}
86+
87+
// Set the necessary headers
88+
req.Header.Set("Content-Type", "application/json")
89+
req.Header.Set("x-api-key", c.APIKey)
90+
91+
// Create an HTTP client and send the request
92+
client := &http.Client{}
93+
resp, err := client.Do(req)
94+
if err != nil {
95+
return err
96+
}
97+
defer resp.Body.Close()
98+
99+
// print a response body for human readability
100+
101+
// Check the response status
102+
if resp.StatusCode >= 400 {
103+
body, _ := io.ReadAll(resp.Body)
104+
var response Response
105+
err = json.Unmarshal(body, &response)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return errors.New(response.Detail)
111+
}
112+
113+
return nil
114+
}

0 commit comments

Comments
 (0)