Overseas access: www.kdjingpai.com
Ctrl + D Favorites

Cogency is an open source cognitive architecture tool, hosted on GitHub, designed to help developers build efficient AI agents. It enables multi-step task processing through transparent ReAct loops (Reason, Act, Observe, Generate), with support for real-time streaming output and traceable operations.Cogency offers flexible integration with tools such as web search, weather forecasting, and itinerary planning, making it suitable for the development of intelligent automation applications. Users can customize the agent's personality, tone, and style, such as creating an AI assistant in the style of a "friendly pirate" or a "patient teacher". With support for multiple language models and an embedded backend, the project is easy to install and developers can get up and running quickly, and Cogency is designed to be modular and extensible for development needs ranging from simple tasks to complex workflows.

Function List

  • multistep reasoning: Decompose complex tasks through the ReAct cycle (Reasoning, Acting, Observing, Generating) and generate results step-by-step.
  • tool integration: Support for tools such as web search, weather forecasts, travel information, etc. Developers can customize the tools to extend functionality.
  • Real-time streaming output: Each operation step is displayed in real time for easy debugging and tracking.
  • Personalized Agents: Support for setting the agent's personality, tone and style, such as "friendly pirate" or "encouraging teacher".
  • Multi-language model supportCompatible with many mainstream language models and embedded back-end, flexible to adapt to different development needs.
  • modular design: Provides an easily extensible architecture where developers can quickly add new tools or features.
  • asynchronous operation: Supports asynchronous programming and is suitable for handling highly concurrent tasks.

Using Help

Installation process

To use Cogency, developers need to first install the Python environment (Python 3.8 or above is recommended). The following are the detailed installation steps:

  1. Cloning Project Code::
    Run the following command in a terminal to clone the Cogency repository locally:

    git clone https://github.com/iteebz/cogency.git
    cd cogency
    
  2. Installation of dependencies::
    Use pip to install the Cogency kernel package:

    pip install cogency
    

    If you need to support all language models, embedded backends and tools, run:

    pip install cogency[all]
    
  3. Configuring API Keys::
    Cogency supports multiple language models (e.g. OpenAI). You need to configure the API key, create .env file and add the key:

    echo "OPENAI_API_KEY=sk-..." >> .env
    

    interchangeability sk-... for your actual API key. Keys from other providers are supported, see the official documentation for details.

  4. Verify Installation::
    Run the following code to test if the installation was successful:

    import asyncio
    from cogency import Agent
    async def main():
    agent = Agent("assistant")
    async for chunk in agent.stream("What is 25 * 43?"):
    print(chunk, end="", flush=True)
    asyncio.run(main())
    

    If the output 1075, indicating that the installation was successful.

Functional operation flow

The core of Cogency is intelligent task processing through ReAct loops. Here is how to use the main features:

Creating and Using AI Agents

  1. Initializing a proxy::
    Create a base agent:

    from cogency import Agent
    agent = Agent("assistant")
    

    This can be done by setting the personality,tone cap (a poem) style Customize the agent with parameters. For example, create a "Friendly Pirate" agent:

    pirate = Agent("pirate", personality="friendly pirate who loves coding")
    
  2. Running Tasks::
    utilization stream method runs the task and outputs real-time streaming results. For example, querying a math problem:

    async for chunk in agent.stream("What is 25 * 43?"):
    print(chunk, end="", flush=True)
    

    outputs 1075. For complex tasks such as trip planning:

    async for chunk in agent.stream("Plan a 3-day trip to Tokyo"):
    print(chunk, end="", flush=True)
    

    The agent will automatically call tools (e.g. weather forecast, travel information) to generate a detailed itinerary.

tool integration

Cogency supports built-in tools such as web_search,weather_forecast) and customization tools. The following are the steps to add a custom tool:

  1. Definition tools::
    Create a customized tool, such as getting the city time of the TimezoneTool::

    from cogency import Agent, BaseTool
    class TimezoneTool(BaseTool):
    def __init__(self):
    super().__init__("timezone", "Get time in any city")
    async def run(self, city: str):
    return {"time": f"Current time in {city}: 14:30 PST"}
    def get_schema(self):
    return "timezone(city='string')"
    
  2. Registration Tools::
    Register the tool to the agent:

    agent = Agent("time_assistant", tools=[TimezoneTool()])
    async for chunk in agent.stream("What's the time in London?"):
    print(chunk, end="", flush=True)
    

    outputs Current time in London: 14:30 PSTThe

Personalized Proxy Settings

Cogency allows you to set a personality for the agent. For example, create a "patient teacher" style agent:

teacher = Agent("teacher", personality="patient teacher", tone="encouraging", style="conversational")
async for chunk in teacher.stream("Explain quantum computing"):
print(chunk, end="", flush=True)

The output will explain quantum computing in an encouraging tone and colloquial style.

Debugging and Tracing

Every step of Cogency's operation is traceable. When running a task, the log records every step of reasoning, action, and observation. For example, when running a trip planning task, the log might show:

  • inference: Weather information is needed to plan activities.
  • act: Call weather_forecast("Tokyo 3 days")The
  • heed: Get weather data (sunny, rainy, etc.).
  • generating: Generate trips based on weather data.

Developers can debug agent behavior through logs to ensure that tasks are executed on demand.

caveat

  • Ensure that your network connection is stable; some tools (e.g., web search) require access to the Internet.
  • probe .env The API key in the file is configured correctly.
  • Asynchronous programming requires Python's asyncio Support, familiarity with asynchronous syntax is recommended.
  • The official documentation (located in the GitHub repository of the README.md) provides more examples and advanced usage.

application scenario

  1. Automated Task Processing
    Cogency lends itself to the development of automated workflows. For example, e-commerce platforms can use Cogency agents to automate responses to customer inquiries, combined with web search tools to find product information.
  2. Travel planning
    Users can enter "Plan a three-day trip to Tokyo" and Cogency will automatically call on weather forecasts and travel information tools to generate weather-appropriate itineraries.
  3. Educational aids
    Teacher-style agents can be used to explain complex concepts, such as quantum computing or programming fundamentals, and are suitable for online education platforms or individual learning.
  4. Development Tools Extension
    Developers can add custom tools to Cogency, such as database querying or file management, to extend its functionality in enterprise-class applications.

QA

  1. What language models does Cogency support?
    Cogency supports a variety of mainstream language models, such as OpenAI's GPT family, please refer to the official documentation for the specific support list. Users are required to provide the corresponding API key.
  2. How to debug the behavior of the agent?
    Cogency's ReAct loops are logged at each step, allowing developers to view details of reasoning, actions, and observations to locate problems.
  3. Is programming experience required?
    Basic use requires knowledge of Python programming, especially asynchronous programming. Non-developers can refer to the sample code to get started quickly.
  4. Can it be used offline?
    Some features (e.g. local computing tasks) are supported for offline use, but tools such as web search require an internet connection.
0Bookmarked
0kudos

Recommended

Can't find AI tools? Try here!

Just type in the keyword Accessibility Bing SearchYou can quickly find all the AI tools on this site.

inbox

Contact Us

Top

en_USEnglish