Skip to content

Commit

Permalink
Merge pull request #2 from devalexandre/feat/add-dinamicid
Browse files Browse the repository at this point in the history
feat: add run
  • Loading branch information
devalexandre authored Apr 30, 2024
2 parents e2f18f3 + 2b08876 commit eedd482
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
29 changes: 4 additions & 25 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
)

func TestMain(m *testing.M) {

m.Run()

}
func TestRun(t *testing.T) {
t.Run("use with GenerateFromSinglePrompt", func(t *testing.T) {
t.Skip()

// Create a new client
runId := uuid.New().String()
log.Println("runId: ", runId)
client := NewClient("")
prompt := "The first man to walk on the moon"
llm, err := openai.New()
Expand All @@ -34,7 +32,6 @@ func TestRun(t *testing.T) {
Name: "langsmithgo-chain",
SessionName: "langsmithgo",
RunType: Chain,
RunID: runId,
Inputs: map[string]interface{}{
"prompt": prompt,
},
Expand All @@ -55,7 +52,6 @@ func TestRun(t *testing.T) {
}

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

t.Run("use with Chain", func(t *testing.T) {
t.Skip()

// Create a new client
runId := uuid.New().String()
client := NewClient("")

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

// use 2 chains
t.Run("use with 2 traces", func(t *testing.T) {
t.Skip()
// Create a new client
runId := uuid.New().String()
client := NewClient("")

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

err = client.Run(&RunPayload{
RunID: runId,
Outputs: map[string]interface{}{
"output": out,
},
})

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

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

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

client := NewClient(os.Getenv("LANGSMITH_API_KEY"))
opts := []openai.Option{
openai.WithModel("gpt-3.5-turbo-0125"),
openai.WithEmbeddingModel("text-embedding-3-large"),
Expand Down Expand Up @@ -238,7 +222,6 @@ func TestRun(t *testing.T) {
Name: "langsmithgo-llm",
SessionName: "langsmithgo",
RunType: LLM,
RunID: runId,
Tags: []string{"llm"},
StartTime: startTime,
Inputs: map[string]interface{}{
Expand All @@ -258,9 +241,7 @@ func TestRun(t *testing.T) {
t.Errorf("Error running: %v", err)
}

embdId := uuid.New().String()
// create embedding

startTime = time.Now().UTC()

embedings, err := llm.CreateEmbedding(ctx, []string{"ola", "mundo"})
Expand All @@ -273,8 +254,6 @@ func TestRun(t *testing.T) {
SessionName: "langsmithgo",
RunType: Embedding,
StartTime: startTime,
RunID: embdId,
ParentID: runId,
Tags: []string{"llm"},
Inputs: map[string]interface{}{
"prompt": out.Choices[0].Content, // Ensure 'output' is properly defined and is of type that has a String method
Expand Down
15 changes: 11 additions & 4 deletions cliente.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"io"
"net/http"
"os"
Expand All @@ -24,15 +25,17 @@ func NewClient(apiKey string) *Client {

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

SetRunId(uuid.New().String())

payload := PostPayload{
ID: input.RunID,
ID: GetRunId(),
Name: input.Name,
RunType: input.RunType,
StartTime: time.Now().UTC(),
Inputs: input.Inputs,
SessionName: input.SessionName,
Tags: input.Tags,
ParentId: input.ParentID,
ParentId: GetParentId(),
Extras: input.Extras,
}

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

SetParentId(GetRunId())

return err
}

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

func (c *Client) RunSingle(input *RunPayload) error {
SetRunId(uuid.New().String())
payload := SimplePayload{
PostPayload: PostPayload{ // Especificando a struct PostPayload
ID: input.RunID,
ID: GetRunId(),
Name: input.Name,
RunType: input.RunType,
StartTime: input.StartTime,
Inputs: input.Inputs,
SessionName: input.SessionName,
Tags: input.Tags,
ParentId: input.ParentID,
ParentId: GetParentId(),
Extras: input.Extras,
},
PatchPayload: PatchPayload{ // Especificando a struct PatchPayload
Expand All @@ -108,6 +114,7 @@ func (c *Client) RunSingle(input *RunPayload) error {
}
err = c.Do(BASE_URL, http.MethodPost, jsonData)

SetParentId(GetRunId())
return err
}

Expand Down
30 changes: 30 additions & 0 deletions contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package langsmithgo

import (
"bytes"
"sync/atomic"
"time"
)

const (
BASE_URL = "https://api.smith.langchain.com/runs"
)

var runID, parentID atomic.Value

type Response struct {
Detail string `json:"detail"`
}
Expand Down Expand Up @@ -91,3 +94,30 @@ func (r RunType) MarshalJSON() ([]byte, error) {
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}

// SetRunId sets the run id
func SetRunId(runId string) {
runID.Store(runId)
}

// GetRunId gets the run id
func GetRunId() string {
if runID.Load() == nil {
return ""

}
return runID.Load().(string)
}

// SetParentId sets the parent id
func SetParentId(parentId string) {
parentID.Store(parentId)
}

// GetParentId gets the parent id
func GetParentId() string {
if parentID.Load() == nil {
return ""
}
return parentID.Load().(string)
}

0 comments on commit eedd482

Please sign in to comment.