OllamaがOpenAI互換APIをサポート
OllamaはOpenAI Chat Completions APIとの互換性を内蔵し、ローカル環境でより多くのツールやアプリケーションの利用を可能にした。
キーポイント
OllamaがOpenAI Chat Completions APIとの互換性を実装
ローカル環境でOpenAI互換ツールやアプリケーションをOllamaと連携可能に
Python/JavaScriptライブラリやVercel AI SDK、Autogenなど主要ツールとの連携例を提供
Llama2、Mistral、Code Llamaなど複数モデルに対応
実験的サポートとしての初期リリースであることを明記
影響分析・編集コメントを表示
影響分析
この互換性実装により、開発者はOpenAI API用に設計された既存のツールやアプリケーションを、ローカルで実行可能なオープンソースLLMとシームレスに連携できるようになり、AI開発の柔軟性とプライバシー保護が向上する。特に、Vercel AI SDKやMicrosoft Autogenといった主要フレームワークとの互換性は、エコシステム全体への波及効果が期待される。
編集コメント
OpenAI APIのデファクトスタンダード化が進む中、オープンソースLLMが同フォーマットに追随することで、開発者体験の統一とツールの流用可能性が高まる重要な動向。
オルラマ(Ollama)は、OpenAIのチャット補完APIとの互換性を内蔵し、ローカル環境でより多くのツールやアプリケーションをオルラマと共に利用できるようになりました。これにより、開発者は自身のコンピュータ上で動作する大規模言語モデルを、OpenAI向けに設計された既存のエコシステムを通じて容易に操作できるようになります。
主要なポイントは以下の通りです。
第一に、互換性の実現方法です。オルラマは、OpenAI APIと同じリクエスト形式とレスポンス形式を採用しています。利用時は、エンドポイントのホスト名を「http://localhost:11434」に変更するだけで、OpenAIのライブラリやツールをそのまま流用できます。APIキーは「ollama」と指定する必要がありますが、実際には使用されません。
第二に、具体的な使用方法です。記事では、cURLコマンド、Pythonライブラリ、JavaScriptライブラリを用いた基本的な呼び出し例を提示しています。いずれも、モデル名(例:「llama2」や「mistral」)とメッセージのリストを指定するという、OpenAI APIと同一のフォーマットに従っています。これにより、既存のコードを最小限の修正でオルラマに切り替えることが可能です。
第三に、実用的な応用例として二つのフレームワークが紹介されています。一つは、Vercel AI SDKです。これは会話型ストリーミングアプリケーション構築のためのオープンソースライブラリであり、そのチャット例をオルラマを使用するように簡単に変更できます。もう一つは、マイクロソフトが開発するマルチエージェントアプリケーションフレームワーク「Autogen」です。こちらも設定リストのベースURLとAPIキーを変更するだけで、オルラマ上で動作するエージェント(例:「codellama」モデルを使用したコード生成アシスタント)を構築・実行できます。
要するに、この互換性機能は、オルラマが提供するローカルでのモデル実行環境という利便性と、OpenAI APIの広大な開発者エコシステムとを結びつけるものです。開発者は、クラウド依存を減らしつつ、使い慣れたAPIインターフェースや人気の高いオープンソースツールを継続して利用できるようになり、ローカルLLMアプリケーション開発のハードルが大幅に低下することが期待されます。現時点では実験的なサポートではありますが、その可能性は大きく、開発の選択肢を広げる重要な進展と言えます。
原文を表示
Ollama now has built-in compatibility with the OpenAI Chat Completions API, making it possible to use more tooling and applications with Ollama locally.
Start by downloading Ollama and pulling a model such as Llama 2 or Mistral:
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
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 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:
This is initial experimental support for the OpenAI API. Future improvements under consideration include:
GitHub issues are welcome! For more information, see the OpenAI compatibility docs.
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み