miurla / morphic

An AI-powered search engine with a generative UI
https://morphic.sh
Apache License 2.0
4.98k stars 1.16k forks source link

feat: Ollama Support #189

Closed peperunas closed 4 weeks ago

peperunas commented 1 month ago

Feature Description

It would be great to support Ollama in addition to the OpenAI API.

Ollama API: https://github.com/ollama/ollama/blob/main/docs/api.md

Use Case

Private LLMs

Additional context

Ollama supports part of the OpenAI endpoints (https://github.com/ollama/ollama/blob/main/docs/openai.md). I tried to set the environment variables accordingly but Morphic does not produce any result.


Ollama logs:

On Ollama's side I see a bunch of 400s:

May 31 21:58:12 architect ollama[2285650]: [GIN] 2024/05/31 - 21:58:12 | 400 |        63.5µs |       127.0.0.1 | POST     "/v1/chat/completions"
May 31 21:58:12 architect ollama[2285650]: [GIN] 2024/05/31 - 21:58:12 | 400 |     162.902µs |       127.0.0.1 | POST     "/v1/chat/completions"
May 31 21:58:28 architect ollama[2285650]: [GIN] 2024/05/31 - 21:58:28 | 400 |       83.97µs |       127.0.0.1 | POST     "/v1/chat/completions"
May 31 21:58:28 architect ollama[2285650]: [GIN] 2024/05/31 - 21:58:28 | 400 |      85.871µs |       127.0.0.1 | POST     "/v1/chat/completions"

Morphic logs

On Morphic side this is the error:

n [AI_APICallError]: json: cannot unmarshal array into Go struct field Message.messages.content of type string
    at O.errorSchema.x.z.object.error.x.z.object.message (/var/task/.next/server/chunks/430.js:66:4263)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async v (/var/task/.next/server/chunks/430.js:66:2056)
    at async S.doGenerate (/var/task/.next/server/chunks/430.js:66:7009)
    at async g (/var/task/.next/server/chunks/111.js:85:651569)
    at async E (/var/task/.next/server/chunks/111.js:85:653181)
    at async w (/var/task/.next/server/chunks/766.js:1:32191)
    at async /var/task/.next/server/chunks/766.js:73:1786 {
  url: 'https://ollamahost.com/v1/chat/completions',
  requestBodyValues: {
    model: 'pino:latest',
    logit_bias: undefined,
    user: undefined,
    max_tokens: undefined,
    temperature: undefined,
    top_p: undefined,
    frequency_penalty: undefined,
    presence_penalty: undefined,
    seed: undefined,
    messages: [ [Object], [Object] ],
    tool_choice: { type: 'function', function: [Object] },
    tools: [ [Object] ]
  },
  statusCode: 400,
  responseBody: '{"error":{"message":"json: cannot unmarshal array into Go struct field Message.messages.content of type string","type":"invalid_request_error","param":null,"code":null}}',
  cause: undefined,
  isRetryable: false,
  data: {
    error: {
      message: 'json: cannot unmarshal array into Go struct field Message.messages.content of type string',
      type: 'invalid_request_error',
      param: null,
      code: null
    }
  }
}

It seems that Morphic is sending invalid requests to Ollama?


I started working on a PR to add Ollama's support (#190 ). I'm currently testing it.

miurla commented 1 month ago

I commented on your PR as well, but there are issues with Object generation and Tool streaming in ollama, so I think this is related.

https://github.com/sgomez/ollama-ai-provider?tab=readme-ov-file#tested-models-and-capabilities

miurla commented 1 month ago

These issues are also present in other models like Groq, so it might be a good idea to start by using the Ollama model only for the writer agent.

ref: https://github.com/miurla/morphic/pull/58

miurla commented 4 weeks ago

PR: #214 Issue: #215