Skip to main content

LangChain integration library for AOps

Project description

aops

LangChain integration library for AOps — load agent prompt configurations from the AOps backend and use them directly in LangChain chains.

Requirements

  • Python 3.12+
  • AOps backend running (self-hosted)
  • API key issued from the AOps UI (Agent detail page → New API Key)

Installation

pip install aops

Quick Start

from dotenv import load_dotenv
load_dotenv()

import aops
aops.init(api_key="aops_...")

from aops.langchain import pull
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

system_prompt = pull("my-agent/my-chain")

chain = (
    ChatPromptTemplate.from_messages([
        system_prompt,
        HumanMessagePromptTemplate.from_template("{user_input}"),
    ])
    | ChatOpenAI(model="gpt-4o-mini")
    | StrOutputParser()
)

result = chain.invoke({"user_input": "Hello!"})

Configuration

API Key

AOps API keys embed the server host, so no separate base_url is needed.

aops_{base64(host)}_{token}

Issue a key from the AOps UI: Agent detail page → API Keys → New API Key

aops.init()

Call init() once before using any aops functions:

import aops

aops.init(api_key="aops_...")

Or use environment variables — init() is optional when env vars are set:

# .env
AGENTOPS_API_KEY=aops_...
OPENAI_API_KEY=sk-...
Environment Variable Default Description
AGENTOPS_API_KEY API key (host is parsed from it)
AGENTOPS_BASE_URL parsed from key Override the host embedded in the key
AGENTOPS_API_PREFIX /api/v1 API path prefix
AGENTOPS_CACHE_TTL 300 Prompt cache TTL in seconds (0 = no cache)

API

pull(ref, *, version=None)

Fetch a chain from AOps and return a SystemMessagePromptTemplate.

from aops.langchain import pull

prompt = pull("my-agent/my-chain")          # latest
prompt = pull("my-agent/my-chain", version=2)  # pinned version

The chain's persona and content are merged into a single system message:

# Persona
{persona}

# Content
{content}

content may contain LangChain template variables (e.g. {language}). persona is treated as a fixed string — its braces are escaped automatically.


@chain_prompt(agent_name, chain_name, *, version=None)

Decorator that fetches the prompt and injects it as the first argument.

Function decorator

from aops.langchain import chain_prompt
from langchain_core.prompts import SystemMessagePromptTemplate

@chain_prompt("my-agent", "my-chain")
def answer(prompt: SystemMessagePromptTemplate, user_input: str) -> str:
    return (
        ChatPromptTemplate.from_messages([
            prompt,
            HumanMessagePromptTemplate.from_template("{user_input}"),
        ])
        | ChatOpenAI(model="gpt-4o-mini")
        | StrOutputParser()
    ).invoke({"user_input": user_input})

result = answer(user_input="What is AOps?")

Class decorator

The prompt is injected into __init__ as the first argument after self. Build the chain once and reuse it:

@chain_prompt("my-agent", "my-chain")
class MyAgent:
    def __init__(self, prompt: SystemMessagePromptTemplate) -> None:
        self.chain = (
            ChatPromptTemplate.from_messages([
                prompt,
                HumanMessagePromptTemplate.from_template("{user_input}"),
            ])
            | ChatOpenAI(model="gpt-4o-mini")
            | StrOutputParser()
        )

    def run(self, user_input: str) -> str:
        return self.chain.invoke({"user_input": user_input})

agent = MyAgent()
result = agent.run(user_input="Hello!")

License

MIT

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

aops-0.1.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

aops-0.1.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aops-0.1.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for aops-0.1.1.tar.gz
Algorithm Hash digest
SHA256 edbf56f2257dcfcaf2ba038d327b5db2011e44074f8855ff75123f54ec1778e9
MD5 8d7f51fe16652f3aa3891455d29eed4d
BLAKE2b-256 fb65cf7387fae7470b53d6c37021df0029a21d37d032fa78bd9e143190ed03ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aops-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for aops-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9a8a6ff4b6e51819bb240033a433d4d99a39295dc59c72367bfc7741e4b040e
MD5 540677efef52d68636bbe9f0b2488eb8
BLAKE2b-256 735186ab3ebb5735fc95ff8819266464dd3d411e85d8c1289daa2b6201c1fbeb

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