removed example_basic as it was a duplicate of ExampleClient_Chat
This commit is contained in:
parent
2d2b212e3d
commit
edd08f62ab
@ -40,74 +40,6 @@ func TestMain(m *testing.M) {
|
|||||||
os.Exit(result)
|
os.Exit(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_basic() {
|
|
||||||
// Create a new context
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Create a new client
|
|
||||||
client, err := api.ClientFromEnvironment()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// List all the models on the server as tags
|
|
||||||
tags, err := client.List(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we get an empty list it means that there are no models on the server
|
|
||||||
if len(tags.Models) == 0 {
|
|
||||||
log.Fatal("no tags returned")
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort the tags.models by Size least to greatest
|
|
||||||
sort.Slice(tags.Models, func(i, j int) bool {
|
|
||||||
return tags.Models[i].Size < tags.Models[j].Size
|
|
||||||
})
|
|
||||||
|
|
||||||
// Get the first model in the list
|
|
||||||
model := tags.Models[0].Model
|
|
||||||
|
|
||||||
// now we're getting ready for the big show. CHATTING WITH THE TINIEST MODEL ON THE SERVER!
|
|
||||||
|
|
||||||
// Create a new chat message to send to the server. Role must be defined and can be "user", system", or a third one that I forget.
|
|
||||||
// These should be defined in the api package as an enum constant or something.
|
|
||||||
chatMessage := api.Message{
|
|
||||||
Content: "Hello! Tell me about yourself in exactly one sentence.",
|
|
||||||
Role: "user",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new chat request to send to the server. This specifies the model and the messages to send.
|
|
||||||
req := &api.ChatRequest{
|
|
||||||
Model: model,
|
|
||||||
Messages: []api.Message{chatMessage},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a function to handle the response from the server. This is a callback function that will be called for each response from the server.
|
|
||||||
var modelResponseText string // we will store the response from the server in this variable
|
|
||||||
fn := func(response api.ChatResponse) error { // this callback function fires for each token returned from the server
|
|
||||||
|
|
||||||
modelResponseText += response.Message.Content // append the response to the modelResponseText variable to retrieve the whole message
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := client.Chat(ctx, req, fn); err != nil { // send the request to the server and if there is an error, log it
|
|
||||||
if errors.Is(err, context.Canceled) {
|
|
||||||
log.Fatal("context was canceled")
|
|
||||||
}
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if modelResponseText == "" {
|
|
||||||
log.Fatal("modelResponseText is empty")
|
|
||||||
} else {
|
|
||||||
fmt.Println("model responded")
|
|
||||||
}
|
|
||||||
// Output:
|
|
||||||
// model responded
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleClient_Heartbeat() {
|
func ExampleClient_Heartbeat() {
|
||||||
// Create a new context
|
// Create a new context
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user