Skip to main content

openai-simple-chat

大模型对话标准接口。支持模板对话、JSON对话等。

安装

pip install openai-simple-chat

环境变量配置项

  • OPENAI_BASE_URL # 支持openai兼容服务
  • OPENAI_API_KEY
  • OPENAI_CHAT_MODEL
  • OLLAMA_BASE_URL # 支持ollama兼容服务
  • OLLAMA_API_KEY
  • OLLAMA_CHAT_MODEL
  • OPENAI_SIMPLE_CHAT_TEMPLATE_ENGINE # 其它设置
  • OPENAI_SIMPLE_CHAT_LOGGER_NAME

使用方法

test_templates/calc.txt

以标准json返回以下计算结果数值【输出格式为:{"result": xx}】:{{expression}}

main1.py

import openai_simple_chat

llm = openai_simple_chat.OpenAIChatService(
    template_engine=openai_simple_chat.get_template_prompt_by_jinjia2,
    template_root="test_templates",
)
response, response_info = llm.jsonchat(
    template="calc.txt",
    expression="1+1",
)
assert response
assert response_info
assert isinstance(response, dict)
assert isinstance(response_info, dict)
assert "result" in response
assert response["result"] == 2

# 注意,如果是stream_chat的话,response可能为空字符串。

main2.py

import openai_simple_chat

llm = openai_simple_chat.OllamaChatService(
    template_engine=openai_simple_chat.get_template_prompt_by_jinjia2,
    template_root="test_templates",
)
response, response_info = llm.jsonchat(template="calc.txt", expression="1+1")
assert response
assert response_info
assert isinstance(response, dict)
assert isinstance(response_info, dict)
assert "result" in response
assert response["result"] == 2

版本记录

v0.1.0

  • 版本首发。
  • 支持模板对话。
  • 支持json对话。
  • 兼容openai和ollama服务。
  • 兼容django和jinja2模板引擎。
  • jsonchat已经对deekseek输出的think过程输出进行处理。

v0.1.1

  • OpenAIService服务支持max_tokens参数。

v0.1.2

  • 添加:get_template_prompt_by_django_template_source_engine提示词模板引擎。
  • 改进:更灵活的服务初始化构造和调用。

v0.1.3

  • 修正:直接使用template生成提示词而无需prompt的情况下报错的问题。

v0.1.4

  • 添加:动态计算max_tokens的机制。
  • 修改:移除max_input_tokens和max_output_tokens参数。
  • 修正:openai的streaming_chat问题。
  • 修正:ollama的对话问题。

v0.1.5

  • 优化:非标准json输出的处理,支持半开json块的解析。

v0.1.8

  • 优化:添加extra_messages参数。

Release files for openai-simple-chat 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-simple-chat 0.1.8
File Size Uploaded
openai-simple-chat-0.1.8.tar.gz 16.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-simple-chat 0.1.8
File Interpreter ABI Platform
openai_simple_chat-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 36.1 kB

Release files / openai-simple-chat-0.1.8.tar.gz

Download URL openai-simple-chat-0.1.8.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
bfed634b4375ca1491f8f504fe4cd1d3884721ea5583dfecfac07e6084e83193
BLAKE2b-256 checksum
How to use checksums
7bfaae20840a700a3fe57267f004d5f47b6d9fc2c480078c01ddead1e3e0996a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / openai_simple_chat-0.1.8-py3-none-any.whl

Download URL openai_simple_chat-0.1.8-py3-none-any.whl
Size 20.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f16c59db94e3a5b279a7bab9f6f2a0b818ba5bbe023330e781978786ebf1381
BLAKE2b-256 checksum
How to use checksums
b948b030261fd17eb3d978c8a71a0346b54ae9f3d4af52c49335a56c01e84613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page