First AI generated version

This commit is contained in:
Konrad Neitzel
2026-06-11 20:01:48 +02:00
commit c7d14ffee7
26 changed files with 1884 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
package gollama
import (
"context"
"encoding/json"
"fmt"
)
// CompleteJSON requests JSON output and unmarshals it into v.
func (c *Client) CompleteJSON(ctx context.Context, prompt string, v any, opts ...CallOption) error {
opts = append([]CallOption{WithFormatJSON()}, opts...)
result, err := c.Complete(ctx, prompt, opts...)
if err != nil {
return err
}
if err := json.Unmarshal([]byte(result.Text), v); err != nil {
return fmt.Errorf("unmarshal JSON response: %w", err)
}
return nil
}
// ChatJSON sends messages with JSON format and unmarshals the response into v.
func (c *Client) ChatJSON(ctx context.Context, messages []Message, v any, opts ...CallOption) error {
opts = append([]CallOption{WithFormatJSON()}, opts...)
result, err := c.ChatMessages(ctx, messages, opts...)
if err != nil {
return err
}
if err := json.Unmarshal([]byte(result.Text), v); err != nil {
return fmt.Errorf("unmarshal JSON response: %w", err)
}
return nil
}