Skip to content

Commit 4d00a0f

Browse files
authored
Merge pull request #15 from mikekavouras/update-readme-examples
Update README with additional examples
2 parents 53616c8 + 9b5a860 commit 4d00a0f

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,55 @@ program.Steps[1].Args == []any{"I have been promoted!"}
7171
// You can build a program executor on top of this structure.
7272
```
7373

74+
### Error Handling Example
75+
76+
When working with external services or APIs, it's crucial to handle errors gracefully. Below is an example of how to handle errors when using the `Execute` method of the `Prompt` struct.
77+
78+
```go
79+
ctx := context.Background()
80+
model := ... // your model client
81+
82+
prompt := typechat.NewPrompt[Classifier](model, "Analyze the sentiment of this text.")
83+
result, err := prompt.Execute(ctx)
84+
if err != nil {
85+
fmt.Printf("An error occurred: %s\n", err)
86+
return
87+
}
88+
89+
fmt.Printf("Sentiment analysis result: %s\n", result.Sentiment)
90+
```
91+
92+
This example demonstrates catching and handling errors returned by the `Execute` method, ensuring that your application can respond appropriately to failures.
93+
94+
### Custom Adapter Example
95+
96+
To use a custom adapter with the library, you need to create an adapter that implements the `client` interface. Below is an example of how to create a custom adapter and use it with `NewPrompt`.
97+
98+
```go
99+
type MyCustomAdapter struct {
100+
// Custom fields and methods
101+
}
102+
103+
func (m *MyCustomAdapter) Do(ctx context.Context, prompt []Message) (string, error) {
104+
// Implement the logic to send the prompt to your service and return the response
105+
return "response from your service", nil
106+
}
107+
108+
ctx := context.Background()
109+
adapter := &MyCustomAdapter{}
110+
111+
prompt := typechat.NewPrompt[Classifier](adapter, "Analyze this text with my custom adapter.")
112+
result, err := prompt.Execute(ctx)
113+
if err != nil {
114+
fmt.Printf("An error occurred: %s\n", err)
115+
return
116+
}
117+
118+
fmt.Printf("Custom adapter result: %s\n", result.Sentiment)
119+
```
120+
121+
This example demonstrates creating a custom adapter that implements the `client` interface and using it with `NewPrompt` to send prompts to your custom service.
122+
74123
## Contributing
75124

76125
This library is under development and still requires more work to solidify the provided APIs so use with caution. A release will be done at some point in the near future.
@@ -83,4 +132,4 @@ This library is under development and still requires more work to solidify the p
83132
- Figure out best way to stay in sync with the original TypeChat project
84133
- Setup CI
85134

86-
Get involved! Lots todo!
135+
Get involved! Lots todo!

0 commit comments

Comments
 (0)