[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-70996":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":17,"stars7d":18,"stars30d":19,"stars90d":16,"forks30d":16,"starsTrendScore":20,"compositeScore":21,"rankGlobal":10,"rankLanguage":10,"license":22,"archived":23,"fork":23,"defaultBranch":24,"hasWiki":23,"hasPages":23,"topics":25,"createdAt":10,"pushedAt":10,"updatedAt":27,"readmeContent":28,"aiSummary":29,"trendingCount":16,"starSnapshotCount":16,"syncStatus":30,"lastSyncTime":31,"discoverSource":32},70996,"ollama-python","ollama\u002Follama-python","ollama","Ollama Python library","https:\u002F\u002Follama.com",null,"Python",10147,1075,78,110,0,25,71,180,75,44.1,"MIT License",false,"main",[7,26],"python","2026-06-12 02:02:46","# Ollama Python Library\n\nThe Ollama Python library provides the easiest way to integrate Python 3.8+ projects with [Ollama](https:\u002F\u002Fgithub.com\u002Follama\u002Follama).\n\n## Prerequisites\n\n- [Ollama](https:\u002F\u002Follama.com\u002Fdownload) should be installed and running\n- Pull a model to use with the library: `ollama pull \u003Cmodel>` e.g. `ollama pull gemma3`\n  - See [Ollama.com](https:\u002F\u002Follama.com\u002Fsearch) for more information on the models available.\n\n## Install\n\n```sh\npip install ollama\n```\n\n## Usage\n\n```python\nfrom ollama import chat\nfrom ollama import ChatResponse\n\nresponse: ChatResponse = chat(model='gemma3', messages=[\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n])\nprint(response['message']['content'])\n# or access fields directly from the response object\nprint(response.message.content)\n```\n\nSee [_types.py](ollama\u002F_types.py) for more information on the response types.\n\n## Streaming responses\n\nResponse streaming can be enabled by setting `stream=True`.\n\n```python\nfrom ollama import chat\n\nstream = chat(\n    model='gemma3',\n    messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],\n    stream=True,\n)\n\nfor chunk in stream:\n  print(chunk['message']['content'], end='', flush=True)\n```\n\n## Cloud Models\n\nRun larger models by offloading to Ollama’s cloud while keeping your local workflow.\n\n- Supported models: `deepseek-v3.1:671b-cloud`, `gpt-oss:20b-cloud`, `gpt-oss:120b-cloud`, `kimi-k2:1t-cloud`, `qwen3-coder:480b-cloud`, `kimi-k2-thinking` See [Ollama Models - Cloud](https:\u002F\u002Follama.com\u002Fsearch?c=cloud) for more information\n\n### Run via local Ollama\n\n1) Sign in (one-time):\n\n```\nollama signin\n```\n\n2) Pull a cloud model:\n\n```\nollama pull gpt-oss:120b-cloud\n```\n\n3) Make a request:\n\n```python\nfrom ollama import Client\n\nclient = Client()\n\nmessages = [\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n]\n\nfor part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):\n  print(part.message.content, end='', flush=True)\n```\n\n### Cloud API (ollama.com)\n\nAccess cloud models directly by pointing the client at `https:\u002F\u002Follama.com`.\n\n1) Create an API key from [ollama.com](https:\u002F\u002Follama.com\u002Fsettings\u002Fkeys) , then set:\n\n```\nexport OLLAMA_API_KEY=your_api_key\n```\n\n2) (Optional) List models available via the API:\n\n```\ncurl https:\u002F\u002Follama.com\u002Fapi\u002Ftags\n```\n\n3) Generate a response via the cloud API:\n\n```python\nimport os\nfrom ollama import Client\n\nclient = Client(\n    host='https:\u002F\u002Follama.com',\n    headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')}\n)\n\nmessages = [\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n]\n\nfor part in client.chat('gpt-oss:120b', messages=messages, stream=True):\n  print(part.message.content, end='', flush=True)\n```\n\n## Custom client\nA custom client can be created by instantiating `Client` or `AsyncClient` from `ollama`.\n\nAll extra keyword arguments are passed into the [`httpx.Client`](https:\u002F\u002Fwww.python-httpx.org\u002Fapi\u002F#client).\n\n```python\nfrom ollama import Client\nclient = Client(\n  host='http:\u002F\u002Flocalhost:11434',\n  headers={'x-some-header': 'some-value'}\n)\nresponse = client.chat(model='gemma3', messages=[\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n])\n```\n\n## Async client\n\nThe `AsyncClient` class is used to make asynchronous requests. It can be configured with the same fields as the `Client` class.\n\n```python\nimport asyncio\nfrom ollama import AsyncClient\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  response = await AsyncClient().chat(model='gemma3', messages=[message])\n\nasyncio.run(chat())\n```\n\nSetting `stream=True` modifies functions to return a Python asynchronous generator:\n\n```python\nimport asyncio\nfrom ollama import AsyncClient\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  async for part in await AsyncClient().chat(model='gemma3', messages=[message], stream=True):\n    print(part['message']['content'], end='', flush=True)\n\nasyncio.run(chat())\n```\n\n## API\n\nThe Ollama Python library's API is designed around the [Ollama REST API](https:\u002F\u002Fgithub.com\u002Follama\u002Follama\u002Fblob\u002Fmain\u002Fdocs\u002Fapi.md)\n\n### Chat\n\n```python\nollama.chat(model='gemma3', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])\n```\n\n### Generate\n\n```python\nollama.generate(model='gemma3', prompt='Why is the sky blue?')\n```\n\n### List\n\n```python\nollama.list()\n```\n\n### Show\n\n```python\nollama.show('gemma3')\n```\n\n### Create\n\n```python\nollama.create(model='example', from_='gemma3', system=\"You are Mario from Super Mario Bros.\")\n```\n\n### Copy\n\n```python\nollama.copy('gemma3', 'user\u002Fgemma3')\n```\n\n### Delete\n\n```python\nollama.delete('gemma3')\n```\n\n### Pull\n\n```python\nollama.pull('gemma3')\n```\n\n### Push\n\n```python\nollama.push('user\u002Fgemma3')\n```\n\n### Embed\n\n```python\nollama.embed(model='gemma3', input='The sky is blue because of rayleigh scattering')\n```\n\n### Embed (batch)\n\n```python\nollama.embed(model='gemma3', input=['The sky is blue because of rayleigh scattering', 'Grass is green because of chlorophyll'])\n```\n\n### Ps\n\n```python\nollama.ps()\n```\n\n## Errors\n\nErrors are raised if requests return an error status or if an error is detected while streaming.\n\n```python\nmodel = 'does-not-yet-exist'\n\ntry:\n  ollama.chat(model)\nexcept ollama.ResponseError as e:\n  print('Error:', e.error)\n  if e.status_code == 404:\n    ollama.pull(model)\n```\n","Ollama Python库简化了Python 3.8+项目与Ollama平台的集成过程。其核心功能包括支持本地和云端模型调用、响应流式处理以及通过API直接访问云服务，能够帮助开发者轻松实现对话式AI应用的开发。技术上，该库提供了简洁直观的API接口，并且支持多种大型语言模型，如gpt-oss:120b-cloud等。适用于需要快速构建基于自然语言处理的应用场景，比如聊天机器人、智能客服系统或任何涉及文本生成与理解的服务。",2,"2026-06-11 03:35:21","high_star"]