Skip to main content

Think LLM Client

一个灵活的 LLM 和 VLM 模型交互 SDK,支持基础的模型交互和 CLI 界面。

特性

  • 支持多种模型类型(LLM、VLM)
  • 支持多个提供商和模型
  • 提供基础的模型交互接口
  • 提供丰富的 CLI 界面
  • 支持图片分析和比较
  • 支持流式输出和思维链
  • 支持对话历史管理
  • 类型提示和文档完备

安装

使用 uv 安装(推荐):

uv pip install think-llm-client

或使用传统的 pip:

pip install think-llm-client

快速开始

基础用法

import asyncio
from think_llm_client import LLMClient

async def main():
    # 创建客户端
    client = LLMClient()
    
    # 设置模型
    client.set_model("llm", "openai", "gpt-4")
    
    # 基础对话
    reasoning, response = await client.chat("Python 中的装饰器是什么?")
    print(f"回答:{response}")
    
    # 图片分析
    reasoning, response = await client.analyze_image(
        "image.jpg",
        "分析这个产品的优缺点"
    )
    print(f"图片分析:{response}")

if __name__ == "__main__":
    asyncio.run(main())

CLI 界面

# 启动交互式对话
python -m think_llm_client.cli chat

# 分析图片
python -m think_llm_client.cli analyze image.jpg "描述这个图片"

配置

配置文件位置

配置文件可以放置在以下位置:

  1. 项目根目录的 config.json
  2. 用户目录下的 .think_llm_client/config.json

你可以在创建客户端时指定配置文件路径:

from think_llm_client import LLMClient

client = LLMClient(config_path="/path/to/your/config.json")

配置文件格式

配置文件使用 JSON 格式,支持配置多种模型类型(LLM、VLM)和多个提供商:

{
  "model_types": {
    "llm": {
      "providers": {
        "openai": {
          "api_key": "your-api-key",
          "api_url": "https://api.openai.com/v1",
          "model": {
            "gpt-4": {
              "max_tokens": 2000,
              "system_prompt": "你是一个有帮助的助手"
            },
            "gpt-3.5-turbo": {
              "max_tokens": 1000
            }
          }
        }
      }
    },
    "vlm": {
      "providers": {
        "openai": {
          "api_key": "your-api-key",
          "api_url": "https://api.openai.com/v1",
          "model": {
            "gpt-4-vision-preview": {
              "max_tokens": 1000
            }
          }
        }
      }
    }
  }
}

或者使用环境变量:

export OPENAI_API_KEY=your-api-key

详细使用说明

1. 基础对话

import asyncio
from think_llm_client import LLMClient

async def main():
    # 创建客户端
    client = LLMClient()
    
    # 设置模型
    client.set_model("llm", "openai", "gpt-4")
    
    # 基础对话(默认使用流式输出)
    reasoning, response = await client.chat("Python 中的装饰器是什么?")
    print(f"思维过程:{reasoning}")
    print(f"回答:{response}")
    
    # 非流式对话
    reasoning, response = await client.chat(
        "给我一个装饰器的例子",
        stream=False
    )

if __name__ == "__main__":
    asyncio.run(main())

2. 图片分析

async def analyze_images():
    client = LLMClient()
    client.set_model("vlm", "openai", "gpt-4-vision")
    
    # 分析单张图片
    reasoning, response = await client.analyze_image(
        "product.jpg",
        "分析这个产品的优缺点"
    )
    
    # 比较多张图片
    reasoning, response = await client.compare_images(
        ["image1.jpg", "image2.jpg"],
        "比较这两张图片的区别"
    )

3. 对话历史管理

async def manage_chat_history():
    client = LLMClient()
    client.set_model("llm", "openai", "gpt-4")
    
    # 进行对话
    await client.chat("你好")
    await client.chat("今天天气不错")
    
    # 保存对话历史
    client.save_chat_history("my_chat.json")
    
    # 清除当前对话历史
    client.clear_history()
    
    # 加载之前的对话历史
    client.load_chat_history_from_file("my_chat.json")
    
    # 获取可用的历史记录
    histories = client.get_available_histories()
    for path, timestamp in histories:
        print(f"历史记录:{path}, 时间:{timestamp}")

4. 流式输出处理

async def handle_stream():
    client = LLMClient()
    client.set_model("llm", "openai", "gpt-4")
    
    async for type_, chunk, full_content in client.chat_stream("讲个故事"):
        if type_ == "reasoning":
            print(f"思维过程: {chunk}", end="")
        else:
            print(f"内容: {chunk}", end="")

CLI 使用

基础对话

# 启动交互式对话
python -m think_llm_client.cli chat

# 指定模型进行对话
python -m think_llm_client.cli chat --model-type llm --provider openai --model gpt-4

图片分析

# 分析单张图片
python -m think_llm_client.cli analyze image.jpg "描述这个图片"

# 比较多张图片
python -m think_llm_client.cli compare image1.jpg image2.jpg "比较这两张图片的区别"

高级特性

流式输出

async def main():
    client = LLMClient()
    client.set_model("llm", "openai", "gpt-4")
    
    # 启用流式输出
    async for chunk_type, chunk, full_content in client.chat_stream("解释量子计算"):
        if chunk_type == "reasoning":
            print(f"思维过程: {chunk}", end="")
        elif chunk_type == "content":
            print(f"回答: {chunk}", end="")

对话历史管理

# 保存对话历史
client.save_history("chat_history.json")

# 加载对话历史
client.load_history_from_file("chat_history.json")

开发

使用 uv 安装开发依赖(推荐):

uv pip install -e ".[dev]"

或使用传统的 pip:

pip install -e ".[dev]"

运行测试和代码检查:

# 运行测试
pytest

# 代码格式化
black .
ruff check .
mypy .

添加新的 Git 标签并推送到 GitHub

  1. 确保所有更改已提交并推送到主分支:

    git add .
    git commit -m "Your commit message"
    git push origin main
    
  2. 创建新的 Git 标签:

    git tag vX.Y.Z
    
  3. 推送标签到 GitHub:

    git push origin vX.Y.Z
    

许可证

MIT License

Metadata

Release files for think-llm-client 0.4.1

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

Source distribution (sdist)

Source distribution for think-llm-client 0.4.1
File Size Uploaded
think_llm_client-0.4.1.tar.gz 56.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for think-llm-client 0.4.1
File Interpreter ABI Platform
think_llm_client-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 78.9 kB

Release files / think_llm_client-0.4.1.tar.gz

Download URL think_llm_client-0.4.1.tar.gz
Size 56.8 kB
Tags Source
SHA-256 checksum
How to use checksums
83ff1801d2f546a6e52ded5a373cb4349b4cfc050e6daf755b0b194a219b74e8
BLAKE2b-256 checksum
How to use checksums
1421ed4a824d234b12c4e6eb3c98769b89b36b264dfcebe8749bd3dce50eb9d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / think_llm_client-0.4.1-py3-none-any.whl

Download URL think_llm_client-0.4.1-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1aa532348d23c0713c844c1f2dc8a83aa24e8506a1f3c629dd0c151c0472a72a
BLAKE2b-256 checksum
How to use checksums
4991284d4cec7161be01f6d329fcd84c3fdc01704cc1c7e1d894d4fa1971e6ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

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