avatar

技术小站

老骥伏枥,日行千里

  • 首页
  • Java
  • Web
  • 框架
  • 微服务
  • 架构
  • 数据库
  • 中间件
  • 研发
  • 关于
Home Ollama API OpenAI compatibility
文章

Ollama API OpenAI compatibility

Posted 2024-07-27 Updated 2024-07- 27
By 管理员
7~9 min read

原文:OpenAI compatibility · Ollama Blog
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)

AI
AI
License:  CC BY 4.0
Share

Further Reading

Jun 4, 2025

Proxy AI 配置第三方AI模型

DeepSeek的配置 Chat Completions endpoint to https://api.deepseek.com/chat/completions deepseek-reasoner Code Completions endpoint to https://api.deepseek

Feb 9, 2025

在IDEA中CodeGPT使用DeepSeek集成

引言 2025年的春节可以说是人工智能在中国史上飘红的一段历史时刻,年后上班的第一天,便马不停蹄的尝试新技能。今天的科技在飞速发展,编程领域的人工智能工具犹如雨后春笋般涌现。 其中,DeepSeek 则以其卓越的性能和智能化的功能,迅速在众多开发者中赢得了青睐。对于 Java 开发者而言,将 Dee

Feb 9, 2025

Continue开源AI编程助手:堪比Copilot的VSCode最强生产力插件

引言 在之前的文章里,我经常介绍关于Cline的使用,不过作为开源方案,Cline主要关注的是Agent的能力,而对于Editor的功能,比如自动补全、inline编辑等,则没有关注,所以今天给大家介绍另一款强大VSCode编程插件Continue。它的主要特点则是通过AI辅助来提升开发效率,帮助开

OLDER

docker: Error response from daemon: Ports are not available

NEWER

Navicat Premium v16、v17 破解激活

Recently Updated

  • 【Windows】ApifoxAppAgent开机启动项删除
  • Proxy AI 配置第三方AI模型
  • Windows11 莫名端口占用,而又找不到占用应用的奇怪问题
  • Crack Sublime Text 4.2.0.0 Build 4200
  • 当 SSH 登录 CentOS 7 系统时认证缓慢的解决方案

Trending Tags

Halo 研发 工具 Linux 数据库 Java AI 象棋 Web Docker

Contents

©2025 技术小站. Some rights reserved.

Using the Halo theme Chirpy