ollama+autogen

#原文地址

OpenAI compatibility
February 8, 2024
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.

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Autogen multiagent是一种自动生成多智能体系统的方法。 在传统的多智能体系统中,需要手动设计和编写每个智能体的行为规则和决策策略。这样做的问题是,当系统需要扩展或修改时,需要手动调整每个智能体的规则和策略,非常耗时和困难。而autogen multiagent方法则通过自动生成智能体系统的规则和策略,极大地简化了系统的设计和维护过程。 具体实现autogen multiagent的方法有多种。一种常用的方法是基于机器学习和优化算法。系统首先采用机器学习算法对智能体的行为规则进行训练,让系统能够从大量的实例中学习合适的决策策略。然后,使用优化算法对系统中的智能体进行优化,并自动调整它们的规则和策略,以实现更高的性能和效率。 另一种实现autogen multiagent的方法是基于进化算法。系统首先通过随机生成一组智能体的规则和策略作为初始种群,然后使用进化算法对这些智能体进行迭代优化。在每一代中,系统通过评估智能体的性能选择出适应度高的个体,并通过交叉和变异等遗传操作生成新的智能体。通过不断迭代优化,系统可以自动生成合适的智能体规则和策略。 总的来说,autogen multiagent实现是一种通过机器学习、优化算法或进化算法等方法自动生成多智能体系统的规则和策略的方法。它可以大大简化多智能体系统的设计和维护过程,并且能够在系统性能和效率方面取得更好的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值