Skip to main content

Experimental agent framework built on top of LangChain

Project description

CogniWeave

CogniWeave is an experimental agent framework built on top of LangChain. The repository showcases how to combine short‑term memory, persistent chat history and a long‑term vector store with end‑of‑conversation detection. The code base mainly serves as a set of runnable components used by the demonstration scripts and tests.

Features

  • Extensible chat agent – defaults to OpenAI models but can be switched to other providers via environment variables.
  • Persistent chat history – messages are stored in a SQLite database for later analysis and memory generation.
  • Vectorised long‑term memory – FAISS indexes store tagged long‑term memory and allow retrieval as the conversation evolves.
  • Automatic memory creation – short and long‑term memories are generated when a session ends and merged into the history.
  • Interactive CLI – run python -m cogniweave demo to try the full pipeline from the terminal.

Additional helper functions for building the pipeline are available in the cogniweave.quickstart module.

Environment variables

The agent relies on several environment variables. Reasonable defaults are used when a variable is not provided.

Variable Purpose Default
CHAT_MODEL Chat model in the form provider/model openai/gpt-4.1
AGENT_MODEL Agent model in the form provider/model openai/gpt-4.1
EMBEDDINGS_MODEL Embedding model in the form provider/model openai/text-embedding-ada-002
SHORT_MEMORY_MODEL Model used to summarise recent messages openai/gpt-4.1-mini
LONG_MEMORY_MODEL Model used for long‑term memory extraction openai/gpt-o3
END_DETECTOR_MODEL Model that decides when a conversation is over openai/gpt-4.1-mini

Model providers usually require credentials such as *_API_KEY and *_API_BASE. These can be supplied via a .env file in the project root.

Environment variables are case-insensitive and override any value defined in config.toml. All settings can be provided entirely through environment variables. Nested options use __ to separate levels, for example:

PROMPT_VALUES__CHAT__EN="You are a helpful assistant."

is equivalent to the configuration file section:

[prompt_values.chat]
en = "You are a helpful assistant."

Configuration file

In addition to environment variables, settings can be defined in a config.toml (or JSON/YAML) file. The CLI automatically loads this file when present, or you can explicitly provide a path with --config-file or by calling cogniweave.init_config(_config_file=...) in your own code.

index_name = "demo"
folder_path = "./.cache"
language = "en"
chat_model = "openai/gpt-4.1"
chat_temperature = 0.8

Any fields matching the keys of the :class:cogniweave.config.Config model are accepted, including nested prompt_values sections for overriding system prompts.

All prompt_values strings support the f-string placeholder {default}. The placeholder is replaced with CogniWeave's built-in prompt so you can extend it easily:

[prompt_values.end_detector]
en = "The agent's name is CogniWeave. {default}"

which becomes "The agent's name is CogniWeave. You are a "message completeness detector. ...".

If you supply a configuration file or define nested options via environment variables, make sure to call cogniweave.init_config() before invoking build_pipeline() so the settings take effect.

Multi-language support

The built-in prompt templates only include Chinese and English text. To use another language, define the prompt in the prompt_values section and set the language key to match. For Japanese using a TOML config:

language = "jp"

[prompt_values.chat]
jp = "あなたは役に立つアシスタントです。"

When a configuration file or environment variables include nested values like this, remember to call cogniweave.init_config() before creating the pipeline so the custom prompts are applied.

CLI usage

After installing the dependencies (see pyproject.toml), start the interactive demo with:

python -m cogniweave demo

You can specify a session identifier to keep conversations separate:

python -m cogniweave demo my_session

Additional options control where history and vector data are stored:

python -m cogniweave demo my_session --index my_index --folder /tmp/cache

You can load custom configuration from a file using the --config-file argument:

python -m cogniweave demo my_session --config-file config.toml

The --index argument sets the file names for the SQLite database and FAISS index, while --folder chooses the directory used to store them. The optional --config-file points to a toml, json or yaml file that contains all the necessary settings for the demo.

Quick build

The quickstart.py module assembles the entire pipeline for you:

from cogniweave import init_config, build_pipeline

init_config()
pipeline = build_pipeline(index_name="demo")

The pipeline object exposes a LangChain Runnable that contains the agent, history store and vector store ready to use.

Manual build

For full control you can construct the components step by step.

  1. Create embeddings

    from cogniweave.quickstart import create_embeddings
    
    embeddings = create_embeddings()
    
  2. Create history store

    from cogniweave.quickstart import create_history_store
    
    history_store = create_history_store(index_name="demo")
    
  3. Create vector store

    from cogniweave.quickstart import create_vector_store
    
    vector_store = create_vector_store(embeddings, index_name="demo")
    
  4. Create chat agent

    from cogniweave.quickstart import create_agent
    
    agent = create_agent()
    
  5. Wire up memory and end detection

    from cogniweave.core.runnables.memory_maker import RunnableWithMemoryMaker
    from cogniweave.core.runnables.end_detector import RunnableWithEndDetector
    from cogniweave.core.runnables.history_store import RunnableWithHistoryStore
    from cogniweave.core.end_detector import EndDetector
    from cogniweave.core.time_splitter import TimeSplitter
    
    pipeline = RunnableWithMemoryMaker(
        agent,
        history_store=history_store,
        vector_store=vector_store,
        input_messages_key="input",
        history_messages_key="history",
        short_memory_key="short_memory",
        long_memory_key="long_memory",
    )
    pipeline = RunnableWithEndDetector(
        pipeline,
        end_detector=EndDetector(),
        default={"output": []},
        history_messages_key="history",
    )
    pipeline = RunnableWithHistoryStore(
        pipeline,
        history_store=history_store,
        time_splitter=TimeSplitter(),
        input_messages_key="input",
        history_messages_key="history",
    )
    
  6. Stream messages

    for chunk in pipeline.stream({"input": "Hello"}, config={"configurable": {"session_id": "demo"}}):
        print(chunk, end="")
    

With these steps you can tailor the pipeline to your own requirements.

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

cogniweave-0.1.1.tar.gz (72.9 kB view details)

Uploaded Source

Built Distribution

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

cogniweave-0.1.1-py3-none-any.whl (93.8 kB view details)

Uploaded Python 3

File details

Details for the file cogniweave-0.1.1.tar.gz.

File metadata

  • Download URL: cogniweave-0.1.1.tar.gz
  • Upload date:
  • Size: 72.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for cogniweave-0.1.1.tar.gz
Algorithm Hash digest
SHA256 48af9a0af061863ac1347fd14ab2171c595b383f70edbcd0825570f4f5aa94e4
MD5 35ea879a73ddb0c628cb8470c09507c7
BLAKE2b-256 68e5c71be997c204b7b7425c8e1406520836a06d094e4d4e3f848a7672aea323

See more details on using hashes here.

File details

Details for the file cogniweave-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cogniweave-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 93.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for cogniweave-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28874540397958ee4d97f6d1f5648959912840e480b8b733056b449a442ffd42
MD5 c691b431ee3b6034ee3bc490ee0adb4f
BLAKE2b-256 fbab4d4f539c2d2bebf246338aef5353d008cf5416a2fd086c304be76205832f

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