]> git.giacomolli.uk - onechat/blobdiff - chain.go
Log daemon events at their slog levels
[onechat] / chain.go
index 3ab9fe114e7d4a3d09c459bc7be534ec89d7943c..c4a4302472d030ee944af5d7bc33f1e96ed7d9f1 100644 (file)
--- a/chain.go
+++ b/chain.go
@@ -43,10 +43,11 @@ func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
                request.Input = input
                response, err := c.client.Create(ctx, request)
                if err != nil {
                request.Input = input
                response, err := c.client.Create(ctx, request)
                if err != nil {
-                       return "", err
+                       return "", fmt.Errorf("create model response: %w", err)
                }
 
                var text string
                }
 
                var text string
+
                input, text, err = c.handle(ctx, input, response)
                if err != nil {
                        return "", err
                input, text, err = c.handle(ctx, input, response)
                if err != nil {
                        return "", err
@@ -63,8 +64,11 @@ func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
 // the messages of take. After the final answer it returns no input, and the
 // replies of the answer as its text.
 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
 // the messages of take. After the final answer it returns no input, and the
 // replies of the answer as its text.
 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
-       var outputs []any
-       var replies []string
+       var (
+               outputs []any
+               replies []string
+       )
+
        for _, raw := range response.Output {
                item, err := chainInputItem(raw)
                if err != nil {
        for _, raw := range response.Output {
                item, err := chainInputItem(raw)
                if err != nil {
@@ -74,6 +78,7 @@ func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse)
                input = append(input, item)
 
                var call ChainFunctionCall
                input = append(input, item)
 
                var call ChainFunctionCall
+
                err = json.Unmarshal(raw, &call)
                if err != nil {
                        return nil, "", fmt.Errorf("decode output item: %w", err)
                err = json.Unmarshal(raw, &call)
                if err != nil {
                        return nil, "", fmt.Errorf("decode output item: %w", err)
@@ -144,6 +149,7 @@ type ChainFunctionCallOutput struct {
 // id.
 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
        var item map[string]any
 // id.
 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
        var item map[string]any
+
        err := json.Unmarshal(raw, &item)
        if err != nil {
                return nil, fmt.Errorf("decode output item: %w", err)
        err := json.Unmarshal(raw, &item)
        if err != nil {
                return nil, fmt.Errorf("decode output item: %w", err)