跳转到主要内容

使用LLM创建具有思考能力的程序。

项目描述

Think

Think是一个用于创建思考程序的Python包。

它提供了简单但强大的原语,用于将大型语言模型(LLM)集成到您的Python程序中。

示例

将AI作为普通函数使用

from think.llm.openai import ChatGPT
from think.ai import ai

@ai
def haiku(topic: str) -> str:
    """
    Write a haiku about {{ topic }}
    """

llm = ChatGPT()

print(haiku(llm, topic="computers"))

允许AI使用工具

from datetime import date

from think.llm.openai import ChatGPT
from think.chat import Chat
from think.tool import tool

@tool
def current_date() -> str:
    """
    Get the current date.

    :returns: current date in YYYY-MM-DD format
    """
    return date.today().isoformat()


llm = ChatGPT()
chat = Chat("You are a helpful assistant.")
chat.user("How old are you (in days since your knowledge cutoff)?")

print(llm(chat, tools=[current_date]))

解析AI输出

import json
from pydantic import BaseModel
from think.llm.openai import ChatGPT
from think.chat import Chat
from think.parser import JSONParser


class CityInfo(BaseModel):
    name: str
    country: str
    population: int
    latitude: float
    longitude: float


llm = ChatGPT()
parser = JSONParser(spec=CityInfo)
chat = Chat(
    "You are a hepful assistant. Your task is to answer questions about cities, "
    "to the best of your knowledge. Your output must be a valid JSON conforming to "
    "this JSON schema:\n" + json.dumps(parser.schema)
).user(city)

answer = llm(chat, parser=parser)

print(f"{answer.name} is a city in {answer.country} with {answer.population} inhabitants.")
print(f"It is located at {answer.latitude} latitude and {answer.longitude} longitude.")

快速入门

通过pip安装

pip install think-llm

请注意,包名是think-llm不是 think

设置您的LLM凭据(OpenAI或Anthropic,具体取决于您想使用的LLM)

export OPENAI_API_KEY=<your-openai-key>
export ANTHROPIC_API_KEY=<your-anthropic-key>

然后您就可以开始使用了

from think.llm.openai import ChatGPT
from think.chat import Chat

llm = ChatGPT()
chat = Chat("You are a helpful assistant.").user("Tell me a funny joke.")
print(llm(chat))

探索示例目录以获取更多使用示例,以及如何使用库的源代码(直到我们构建了适当的文档!如果您想帮忙,请参见以下内容)。

路线图

计划在近期内实现的功能和功能

  • 文档
  • 对Anthropic的全面支持(工具、解析器、AI函数)
  • 通过LiteLLM或其他类似方式支持其他LLM API
  • 通过HuggingFace支持本地LLM
  • 更多示例

如果您想帮助任何这些,请查看开放问题,加入对话并提交PR。请阅读以下贡献部分。

贡献

欢迎贡献!

为确保您的贡献被接受,请遵循以下指南

  • 在开始工作之前,打开一个问题来讨论您的想法,或者如果已经有了关于您想法的问题,请加入那里的对话并解释您计划如何实现它
  • 确保您的代码有良好的文档(文档字符串、类型注解、注释等)并且经过测试(测试覆盖率应该只上升)
  • 确保您的代码使用 ruff(默认设置)进行格式化和类型检查

版权

版权(C)2023-2024. Senko Rasic 和 Think 贡献者。您可以在 MIT 许可证的条款下使用和/或分发此项目。有关更多详细信息,请参阅 LICENSE 文件。

项目详情


下载文件

下载适合您平台文件的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

think_llm-0.0.5.tar.gz (13.0 kB 查看哈希值)

上传时间

构建分发

think_llm-0.0.5-py3-none-any.whl (13.9 kB 查看哈希值)

上传时间 Python 3

支持者: