firebase / genkit

An open source framework for building AI-powered apps with familiar code-centric patterns. Genkit makes it easy to develop, integrate, and test AI features with observability and evaluations. Genkit works with various models and platforms.
Apache License 2.0
817 stars 122 forks source link

[JS] TypeError fetch failed. #194

Closed hemanth closed 5 months ago

hemanth commented 6 months ago

Describe the bug

fetch failed

name: 'TypeError',
  message: 'fetch failed',
  stack: 'TypeError: fetch failed\n' +
    '    at Object.fetch (node:internal/deps/undici/undici:11731:11)\n' +
    '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)'

To Reproduce

import { generate } from '@genkit-ai/ai';
import { configureGenkit } from '@genkit-ai/core';
import { defineFlow, startFlowsServer } from '@genkit-ai/flow';
import * as z from 'zod';
import { ollama } from 'genkitx-ollama';

export default configureGenkit({
  plugins: [
    ollama({
      models: [
        {
          name: 'gemma',
          type: 'generate', // type: 'chat' | 'generate' | undefined
        },
      ],
      serverAddress: 'http://127.0.0.1:11434', // default local address
    }),
  ],
  logLevel: 'debug',
  enableTracingAndMetrics: true,
});

export const dadJokeFlow = defineFlow(
  {
    name: 'dadJokeFlow',
    inputSchema: z.string(),
    outputSchema: z.string(),
  },
  async (topic) => {
    const llmResponse = await generate({
      prompt: `Give me a dad joke for ${topic}`,
      model: 'ollama/gemma',
      config: {
        temperature: 1,
      },
    });

    return llmResponse.text();
  }
);

startFlowsServer();

Expected behavior Response from ollama/gemma

Screenshots

image

Runtime (please complete the following information):

Node version

Additional context

Trace

{
    "traceId": "45780520d500979a461453ac21dadb65",
    "spans": {
        "048a83860c076ee3": {
            "spanId": "048a83860c076ee3",
            "traceId": "45780520d500979a461453ac21dadb65",
            "parentSpanId": "be88bc317935cc0a",
            "startTime": 1716068267656,
            "endTime": 1716068267712.037,
            "attributes": {
                "genkit:type": "action",
                "genkit:name": "ollama/gemma",
                "genkit:path": "/dev-run-action-wrapper/dadJokeFlow/dadJokeFlow/ollama/gemma",
                "genkit:input": "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"Give me a dad joke for math\"}]}],\"config\":{\"temperature\":1},\"tools\":[],\"output\":{\"format\":\"text\"}}",
                "genkit:metadata:subtype": "model",
                "genkit:state": "error"
            },
            "displayName": "ollama/gemma",
            "links": [],
            "instrumentationLibrary": {
                "name": "genkit-tracer",
                "version": "v1"
            },
            "spanKind": "INTERNAL",
            "sameProcessAsParentSpan": {
                "value": true
            },
            "status": {
                "code": 2,
                "message": "fetch failed"
            },
            "timeEvents": {
                "timeEvent": [
                    {
                        "time": 1716068267711.6345,
                        "annotation": {
                            "attributes": {
                                "exception.type": "TypeError",
                                "exception.message": "fetch failed",
                                "exception.stacktrace": "TypeError: fetch failed\n    at Object.fetch (node:internal/deps/undici/undici:11731:11)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
                            },
                            "description": "exception"
                        }
                    }
                ]
            }
        },
        "be88bc317935cc0a": {
            "spanId": "be88bc317935cc0a",
            "traceId": "45780520d500979a461453ac21dadb65",
            "parentSpanId": "64913606c2f8c50f",
            "startTime": 1716068267606,
            "endTime": 1716068267718.3179,
            "attributes": {
                "genkit:type": "flow",
                "genkit:name": "dadJokeFlow",
                "genkit:isRoot": true,
                "genkit:path": "/dev-run-action-wrapper/dadJokeFlow/dadJokeFlow",
                "genkit:metadata:flow:execution": "0",
                "genkit:metadata:flow:name": "dadJokeFlow",
                "genkit:metadata:flow:id": "1660b322-ad28-4ace-8906-69342a55c9a9",
                "genkit:metadata:flow:dispatchType": "start",
                "genkit:metadata:flow:state": "error",
                "genkit:input": "\"math\"",
                "genkit:state": "error"
            },
            "displayName": "dadJokeFlow",
            "links": [],
            "instrumentationLibrary": {
                "name": "genkit-tracer",
                "version": "v1"
            },
            "spanKind": "INTERNAL",
            "sameProcessAsParentSpan": {
                "value": true
            },
            "status": {
                "code": 2,
                "message": "fetch failed"
            },
            "timeEvents": {
                "timeEvent": [
                    {
                        "time": 1716068267717.0923,
                        "annotation": {
                            "attributes": {
                                "exception.type": "TypeError",
                                "exception.message": "fetch failed",
                                "exception.stacktrace": "TypeError: fetch failed\n    at Object.fetch (node:internal/deps/undici/undici:11731:11)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
                            },
                            "description": "exception"
                        }
                    }
                ]
            }
        },
        "64913606c2f8c50f": {
            "spanId": "64913606c2f8c50f",
            "traceId": "45780520d500979a461453ac21dadb65",
            "startTime": 1716068267598,
            "endTime": 1716068267723.0461,
            "attributes": {
                "genkit:type": "action",
                "genkit:name": "dadJokeFlow",
                "genkit:path": "/dev-run-action-wrapper/dadJokeFlow",
                "genkit:input": "{\"start\":{\"input\":\"math\"}}",
                "genkit:metadata:subtype": "flow",
                "genkit:metadata:flow:wrapperAction": "true",
                "genkit:output": "{\"flowId\":\"1660b322-ad28-4ace-8906-69342a55c9a9\",\"name\":\"dadJokeFlow\",\"startTime\":1716068267605,\"input\":\"math\",\"cache\":{},\"eventsTriggered\":{},\"blockedOnStep\":null,\"executions\":[{\"startTime\":1716068267606,\"traceIds\":[\"45780520d500979a461453ac21dadb65\"]}],\"operation\":{\"name\":\"1660b322-ad28-4ace-8906-69342a55c9a9\",\"done\":true,\"result\":{\"error\":\"fetch failed\",\"stacktrace\":\"TypeError: fetch failed\\n    at Object.fetch (node:internal/deps/undici/undici:11731:11)\\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\"}},\"traceContext\":\"{\\\"traceId\\\":\\\"45780520d500979a461453ac21dadb65\\\",\\\"spanId\\\":\\\"be88bc317935cc0a\\\",\\\"traceFlags\\\":1}\"}",
                "genkit:state": "success"
            },
            "displayName": "dadJokeFlow",
            "links": [],
            "spanKind": "INTERNAL",
            "parentSpanId": "885a06075d79204f",
            "sameProcessAsParentSpan": {
                "value": true
            },
            "status": {
                "code": 0
            },
            "timeEvents": {
                "timeEvent": []
            },
            "instrumentationLibrary": {
                "name": "genkit-tracer",
                "version": "v1"
            }
        }
    }
}

P.S: Might be an undici issue.

pavelgj commented 6 months ago

Do you have ollama installed and serving on your local machine? What do you see when when you point your browser at http://127.0.0.1:11434

rajat1saxena commented 6 months ago

I am having the same issue.

caprizone6 commented 6 months ago

I am having same issue as well. I have Ollama installed and going to http://127.0.0.1:11434/ shows message Ollama is running

pavelgj commented 6 months ago

Just to make sure, are you running ollama server and genkit start on the same machine (not in IDX, codespace, etc)?

Unfortunately the current error message is not very helpful, I've sent out #205 to address this, hopefully once it's out (in a few days) it'll help with troubleshooting.

pavelgj commented 6 months ago

A better error message is now released in version 0.5.1. Please give it another try.

i14h commented 5 months ago

this seems to be fixed. Let us know if the issues still persists.