- Ollama가 OpenAI Chat Completions API 초기 호환성을 내장해, OpenAI용 도구와 애플리케이션을 로컬 모델에 그대로 연결할 수 있게 됨
- 설치 후
llama2나 mistral 같은 모델을 내려받고, OpenAI 요청 형식을 유지한 채 호스트만 ` 바꾸면 호출 가능함
- OpenAI Python·JavaScript 라이브러리는
base_url/baseURL을 지정하고, 필수지만 사용되지 않는api_key` 값을 넣어 동작함
- Vercel AI SDK 스트리밍 채팅 앱과 Microsoft Autogen 멀티 에이전트 프레임워크를 Ollama에 연결하는 예제가 제공됨
- 현재 지원은 초기 실험적 지원 단계이며, Embeddings API, 함수 호출, 비전 지원, Logprobs 개선은 향후 검토 대상임
OpenAI API 형식으로 Ollama 호출
ollama pull llama2
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 라이브러리는
base_url을 Ollama 로컬 엔드포인트로 지정함
api_key='ollama'는 필수지만 사용되지 않음
from openai import OpenAI
client = OpenAI(
base_url = 'http://localhost:11434/v1',
api_key='ollama',
)
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)
import OpenAI from 'openai'
const openai = new OpenAI({
baseURL: 'http://localhost:11434/v1',
apiKey: 'ollama',
})
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)
예제 통합과 향후 계획
- Vercel AI SDK는 대화형 스트리밍 애플리케이션을 만들기 위한 오픈소스 라이브러리이며, Next.js OpenAI 예제를 Ollama로 바꿔 사용할 수 있음
npx create-next-app --example https://github.com/vercel/ai/tree/main/examples/next-openai example
cd example
app/api/chat/route.ts에서 OpenAI 클라이언트 설정을 Ollama 로컬 엔드포인트로 바꿈
const openai = new OpenAI({
baseURL: 'http://localhost:11434/v1',
apiKey: 'ollama',
});
- 채팅 완료 요청은
llama2 모델과 stream: true를 사용함
const response = await openai.chat.completions.create({
model: 'llama2',
stream: true,
messages,
});
npm run dev
ollama pull codellama
pip install pyautogen
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.")
- 예제는
python example.py로 실행하며, 어시스턴트가 차트를 그리는 코드를 작성하게 함
python example.py
- OpenAI API 지원은 초기 실험적 지원 단계에 있음
- 향후 개선 검토 항목은 Embeddings API, 함수 호출, 비전 지원, Logprobs임
- GitHub 이슈를 받을 수 있으며, 자세한 정보는 OpenAI 호환성 문서에서 확인 가능함