Skip to main content

A simple llm agent

Project description

中文| English

Chan-Agent

一个简洁高效的 Agent 架构,旨在为开发者提供灵活、易用的工具,支持多种 LLM 模型与任务自动化。

特点

  • 轻量级:高效且快速,适合多种应用场景。
  • 兼容多种 LLM 模型:支持主流的 LLM 模型,灵活配置,方便替换。
  • 支持 LLM 任务执行:内置灵活的任务执行功能,可以方便地执行各种 LLM 任务。
  • 支持 Agent 执行:支持 Agent 执行任务,并通过协作提升系统的智能化与自动化水平。

安装

通过 pip 安装:

pip install chan-agent

使用示例

初始化LLM

from chan_agent.llms import get_llm

llm = get_llm(
    llm_type='openai', 
    model_name='gpt-4o-mini', 
    base_url='https://api.openai.com/v1', 
    api_key='your-api-key'
)

创建任务

from chan_agent.task_llm import TaskLLM
from chan_agent.schema import TaskOutputs, TaskInputItem

class TranslationOutput(TaskOutputs):
    translation: str

task = "Translate the following text to French."
rules = ["Keep the original meaning.", "Use formal language."]

task_llm = TaskLLM(llm=llm, task=task, rules=rules, output_model=TranslationOutput)

执行任务

inputs = [
    TaskInputItem(key="text", key_name="Text to translate", value="Hello, how are you?")
]

result = task_llm.call(inputs=inputs)
print(result)

使用工具

from chan_agent.llms import get_llm
from chan_agent.base_agent import BaseAgent
from chan_agent.base_tool import BaseTool, ToolResult
from chan_agent.schema import AgentMessage

llm = get_llm(
    llm_type='openai', 
    model_name='gpt-4o-mini', 
    base_url='https://api.openai.com/v1', 
    api_key='your-api-key'
)


class WeatherTool(BaseTool):
    name = "weather_tool"
    description = "A tool that fetches weather information for a given city."
    parameters = {
        'city': {
            'type': 'string',
            'description': 'city name',
            'required': True
        },
    }

    def call(self, params, **kwargs) -> ToolResult:
        city = params.get("city")
        
        # Return a fixed fake weather response
        return ToolResult(response=f"The weather in {city} is sunny with a high of 25°C.", use_tool_response=False)
        

tools = [WeatherTool()]
agent = BaseAgent(llm=llm, role="Assistant", tools=tools, rules=["Be polite."])

messages = []
messages.append(AgentMessage(role="user", content="Hello, can you tell me the weather in New York?"))

response = agent.chat(messages)
print(response)

说明

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chan_agent-0.0.12.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chan_agent-0.0.12-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file chan_agent-0.0.12.tar.gz.

File metadata

  • Download URL: chan_agent-0.0.12.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for chan_agent-0.0.12.tar.gz
Algorithm Hash digest
SHA256 0d4f429a3123362590572f5c27daac395380ca2174d36acefc24d2c209ade83b
MD5 4d4d82624b4ae03317c7198eba9c40c8
BLAKE2b-256 c1b44422f9979ecc494a596e5c041a0c4fa7627d4e57362c7e8d8d4baa2f9491

See more details on using hashes here.

File details

Details for the file chan_agent-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: chan_agent-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for chan_agent-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4edbb4e9842ebfef2d7f4cf5f84cb739b223a9c457caa494bf17096e7e032e82
MD5 1da73b3480cc55209357c985495d1560
BLAKE2b-256 beee60c5c42e6de06deb8cb7e21b742a54a1ecb6cc4ad4f027ecc4ec0b509fc9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page