OpenAI 兼容性
OpenAI compatibility

原始链接: https://ollama.ai/blog/openai-compatibility

在最近的开发中,Ollama 语言学习软件现在具有与 OpenAI Chat Completions API 兼容的功能。 这允许通过 cURL 以及 Python 和 JavaScript 库进行本地使用,并为使用 Vercel 的 AI SDK 或多代理平台(如 Autogen)的对话流应用程序提供集成机会。 初始实验支持包括嵌入函数、视觉功能、日志概率以及通过 GitHub 问题发出的邀请。 可以拉取可下载的 Ollama 程序(例如 LLama 2 或 Mistral),然后设置 cURL、OpenAI Python 和 Javascript 库的使用示例,并利用 Code Llama 在 Autogen 上使用 Next.js 运行对话示例。 正在考虑的更多功能包括进一步的集成机会,并且未来的改进将很快继续出现。

本地法学硕士特指在较小数据集上训练的模型,资源要求较低,允许用户在自己的机器上托管和使用这些模型。 Ollama 是一个简化这些本地模型使用的生态系统,但这并不一定意味着能够将 RoRocm 等特定 LLM 与 Ollama 一起使用。 至于性能,最终取决于所使用的具体型号。 然而,Ollama 和 LocalGPT 之间的比较并不简单,因为它们都提供了自己的解决方案,并且根据预期的用例可能涉及各种权衡。 因此,有必要对每个系统进行独立的基准测试。
相关文章

原文

OpenAI compatibility

Ollama now has built-in compatibility with the OpenAI Chat Completions API, making it possible to use more tooling and applications with Ollama locally.

Setup

Start by downloading Ollama and pulling a model such as Llama 2 or Mistral:

ollama pull llama2

Usage

cURL

To invoke Ollama’s OpenAI compatible API endpoint, use the same OpenAI format and change the hostname to http://localhost:11434:

curl http://localhost:11434/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "llama2",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Hello!"
            }
        ]
    }'

OpenAI Python library

from openai import OpenAI

client = OpenAI(
    base_url = 'http://localhost:11434/v1',
    api_key='ollama', # required, but unused
)

response = client.chat.completions.create(
  model="llama2",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The LA Dodgers won in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
)
print(response.choices[0].message.content)

OpenAI JavaScript library

import OpenAI from 'openai'

const openai = new OpenAI({
  baseURL: 'http://localhost:11434/v1',
  apiKey: 'ollama', // required but unused
})

const completion = await openai.chat.completions.create({
  model: 'llama2',
  messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})

console.log(completion.choices[0].message.content)

Examples

Vercel AI SDK

The Vercel AI SDK is an open-source library for building conversational streaming applications. To get started, use create-next-app to clone the example repo:

npx create-next-app --example https://github.com/vercel/ai/tree/main/examples/next-openai example
cd example

Then make the following two edits in app/api/chat/route.ts to update the chat example to use Ollama:

const openai = new OpenAI({
  baseURL: 'http://localhost:11434/v1',
  apiKey: 'ollama',
});
const response = await openai.chat.completions.create({
  model: 'llama2',
  stream: true,
  messages,
});

Next, run the app:

npm run dev

Finally, open the example app in your browser at http://localhost:3000:

Autogen

Autogen is a popular open-source framework by Microsoft for building multi-agent applications. For this, example we’ll use the Code Llama model:

ollama pull codellama

Install Autogen:

pip install pyautogen

Then create a Python script example.py to use Ollama with Autogen:

from autogen import AssistantAgent, UserProxyAgent

config_list = [
  {
    "model": "codellama",
    "base_url": "http://localhost:11434/v1",
    "api_key": "ollama",
  }
]

assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})

user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding", "use_docker": False})
user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA and TESLA stock price change YTD.")

Lastly, run the example to have the assistant write the code to plot a chart:

python example.py

More to come

This is initial experimental support for the OpenAI API. Future improvements under consideration include:

  • Embeddings API
  • Function calling
  • Vision support
  • Logprobs

GitHub issues are welcome! For more information, see the OpenAI compatibility docs.

联系我们 contact @ memedata.com