Skip to content

Commit

Permalink
Updated README to message streaming example (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmanpasala authored Mar 16, 2024
1 parent 71e3db7 commit 8906199
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,79 @@ func main() {

## Messages Streaming Example

### Not yet implemented
```go
package main

import (
"fmt"
"os"

"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
)

func main() {
apiKey, ok := os.LookupEnv("ANTHROPIC_API_KEY")
if !ok {
fmt.Printf("missing ANTHROPIC_API_KEY environment variable")
}
client, err := anthropic.NewClient(apiKey)
if err != nil {
panic(err)
}

// Prepare a message request
request := anthropic.NewMessageRequest(
[]anthropic.MessagePartRequest{{Role: "user", Content: "Hello, Good Morning!. How are you today?"}},
anthropic.WithModel[anthropic.MessageRequest](anthropic.Claude3Haiku),
anthropic.WithMaxTokens[anthropic.MessageRequest](20),
anthropic.WithStreaming[anthropic.MessageRequest](true),
)

// Call the Message method
resps, errors := client.MessageStream(request)

for {
select {
case response := <-resps:
if response.Type == "content_block_delta" {
fmt.Println(response.Delta.Text)
}
if response.Type == "message_stop" {
fmt.Println("Message stop")
return
}
case err := <-errors:
fmt.Println(err)
return
}
}
}
```

### Messages Streaming Example Output

```
Good
morning
!
As
an
AI
language
model
,
I
don
't
have
feelings
or
a
physical
state
,
but
```

## Contributing

Expand Down

0 comments on commit 8906199

Please sign in to comment.