Skip to content

Commit

Permalink
docs: refined logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoaguirre committed Feb 19, 2025
1 parent 5a0758e commit e137900
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 5 additions & 6 deletions go/plugins/vertexai/modelgarden/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,21 @@ func toAnthropicRequest(model string, i *ai.ModelRequest) (anthropic.MessageNewP

// configure system prompt (if given)
sysBlocks := []anthropic.TextBlockParam{}
// userBlocks := []anthropic.ContentBlockParamUnion{}
toolBlocks := []anthropic.ContentBlockParamUnion{}
for _, message := range i.Messages {
if message.Role == ai.RoleSystem {
// only text is supported for system messages
sysBlocks = append(sysBlocks, anthropic.NewTextBlock(message.Text()))
} else if message.Content[len(message.Content)-1].IsToolResponse() {
// if the last message is a ToolResponse, the conversation must continue
// and the ToolResponse message must be sent as a user
// see: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#handling-tool-use-and-tool-result-content-blocks
parts, err := convertParts(message.Content)
if err != nil {
return req, err
}
toolBlocks = append(toolBlocks, parts...)
messages = append(messages, anthropic.NewUserMessage(toolBlocks...))
messages = append(messages, anthropic.NewUserMessage(parts...))
} else {
// handle the rest of the messages
parts, err := convertParts(message.Content)
if err != nil {
return req, err
Expand Down Expand Up @@ -311,7 +312,6 @@ func toGenkitResponse(m *anthropic.Message) *ai.ModelResponse {
switch part.Type {
case anthropic.ContentBlockTypeText:
p = ai.NewTextPart(string(part.Text))
fmt.Printf("part: %#v\n\n", p.Text)
case anthropic.ContentBlockTypeToolUse:
p = ai.NewToolRequestPart(&ai.ToolRequest{
Ref: part.ID,
Expand All @@ -325,7 +325,6 @@ func toGenkitResponse(m *anthropic.Message) *ai.ModelResponse {
}

r.Message = msg
fmt.Printf("r.Message: %#v\n\n", r.Message)
r.Usage = &ai.GenerationUsage{
InputTokens: int(m.Usage.InputTokens),
OutputTokens: int(m.Usage.OutputTokens),
Expand Down
12 changes: 8 additions & 4 deletions go/plugins/vertexai/modelgarden/modelgarden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ func TestModelGarden(t *testing.T) {
}

t.Run("invalid model", func(t *testing.T) {
t.Skipf("no streaming support yet")
m := modelgarden.Model(g, modelgarden.AnthropicProvider, "claude-not-valid-v2")
if m != nil {
t.Fatal("model should have been invalid")
}
})

t.Run("model version ok", func(t *testing.T) {
t.Skipf("no streaming support yet")
m := modelgarden.Model(g, modelgarden.AnthropicProvider, "claude-3-5-sonnet-v2")
resp, err := genkit.Generate(ctx, g,
ai.WithConfig(&ai.GenerationCommonConfig{
Temperature: 1,
Version: "claude-3-5-sonnet-v2@20241022",
}),
ai.WithModel(m),
ai.WithSystemPrompt("talk to me like an evil pirate and say ARR several times"),
ai.WithSystemPrompt("talk to me like an evil pirate and say ARR several times but be very short"),
ai.WithMessages(ai.NewUserMessage(ai.NewTextPart("I'm a fish"))),
)
if err != nil {
Expand All @@ -72,6 +74,7 @@ func TestModelGarden(t *testing.T) {
})

t.Run("model version nok", func(t *testing.T) {
t.Skipf("no streaming support yet")
m := modelgarden.Model(g, modelgarden.AnthropicProvider, "claude-3-5-sonnet-v2")
_, err := genkit.Generate(ctx, g,
ai.WithConfig(&ai.GenerationCommonConfig{
Expand All @@ -86,13 +89,14 @@ func TestModelGarden(t *testing.T) {
})

t.Run("media content", func(t *testing.T) {
t.Skipf("no streaming support yet")
i, err := fetchImgAsBase64()
if err != nil {
t.Fatal(err)
}
m := modelgarden.Model(g, modelgarden.AnthropicProvider, "claude-3-5-sonnet-v2")
resp, err := genkit.Generate(ctx, g,
ai.WithSystemPrompt("You are a professional image detective that talks like an evil pirate that does not like tv shows, your task is to tell the name of the character in the image"),
ai.WithSystemPrompt("You are a professional image detective that talks like an evil pirate that does not like tv shows, your task is to tell the name of the character in the image but be very short"),
ai.WithModel(m),
ai.WithMessages(
ai.NewUserMessage(
Expand All @@ -112,9 +116,9 @@ func TestModelGarden(t *testing.T) {
myJokeTool := genkit.DefineTool(
g,
"myJoke",
"When the user asks for a joke, this tool must be used to tell a joke",
"When the user asks for a joke, this tool must be used to generate a joke, try to come up with a joke that uses the output of the tool",
func(ctx *ai.ToolContext, input *any) (string, error) {
return "do you want a joke? okay, here it is: do you want to hear about pizza? nevermind, it's to cheessy", nil
return "why did the chicken cross the road?", nil
},
)
resp, err := genkit.Generate(ctx, g,
Expand Down

0 comments on commit e137900

Please sign in to comment.