Skip to content

About

Async Python client for Corfedo code search and Corfedo AI, with LangChain and Pydantic AI tools.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Corfedo

Corfedo Python SDK

CI PyPI Python 3.11+ License: Apache-2.0 Documentation

Search and read code across every repository your Corfedo workspace indexes, and ask Corfedo AI, from Python.

Corfedo is a self-hosted code search and AI service. It indexes the repositories you choose across your code hosts and answers searches and questions across them, for people and coding agents. This repository holds the async Python client for its public HTTP API, with optional LangChain, Pydantic AI and Microsoft Agent Framework integrations. Python 3.11 or later.

Install

pip install corfedo                    # client and framework-neutral tools
pip install 'corfedo[pydantic-ai]'     # Pydantic AI toolset and model (corfedo.pydantic_ai)
pip install 'corfedo[langchain]'       # LangChain tools and chat model (corfedo.langchain)
pip install 'corfedo[agent-framework]' # Agent Framework tools and chat client (corfedo.agent_framework)

Quick start

In your Corfedo workspace, open Connect › Manage connections, select Create API key with the Search and read code permission and download its key file. Set CORFEDO_API_KEY_FILE to its path. In a script, run the examples inside an async def main() with asyncio.run(main()).

import os
from corfedo import CorfedoClient
from corfedo.models import SearchCode

async with CorfedoClient.from_api_key_file(os.environ["CORFEDO_API_KEY_FILE"]) as client:
    page = await client.search.search(SearchCode(query="where are search requests parsed"))
    for result in page.results:
        if result.kind == "file":
            print(f"{result.repository} {result.path} @{result.revision}")

Bare words find the most relevant files. Text in double quotes matches exactly and a regular expression between slashes matches a pattern; uppercase AND, OR and NOT combine them, and filters such as repo:, file: and lang: narrow the search:

SearchCode(query=r'"RetryPolicy" OR /retr(y|ies)\b/ lang:python')

A person can sign in through the browser instead, for notebooks and local tools:

from corfedo import CorfedoAuthentication, CorfedoClient, HumanAuthenticationOptions

credentials = await CorfedoAuthentication.sign_in(
    HumanAuthenticationOptions(api_base_uri="https://corfedo.example.com/")
)
async with CorfedoClient(credentials) as client:
    ...

Give your model Corfedo's tools

Give the model your application already runs Corfedo's tools:

from pydantic_ai import Agent
from corfedo.pydantic_ai import CorfedoToolset

agent = Agent(model, toolsets=[CorfedoToolset(client)])

corfedo.langchain.get_corfedo_tools(client) returns LangChain structured tools for create_agent, and corfedo.agent_framework.get_corfedo_tools(client) returns Agent Framework function tools. Optionally add corfedo.tools.INSTRUCTIONS to the system prompt so the model knows that masked credentials appear as a run of *. Tools return JSON text; tool failures are CorfedoToolErrors whose messages are safe to show to the model. Other frameworks and provider SDKs use CorfedoTools directly: pass its definitions to the model and its tool calls to execute.

Ask Corfedo AI

With an API key that also has the Ask Corfedo AI permission, or a browser sign-in with the corfedo:ai:invoke and corfedo:repositories:code:read scopes, client.ai sends messages to Corfedo AI and streams its runs:

from corfedo.models import SendAiMessage

async with client.ai.stream(SendAiMessage(text="How are failed payments retried?")) as run:
    async for event in run:
        if event.type == "text.delta":
            print(event.text, end="")
print(run.answer)

Streamed text is for display and can change before the run ends. Use run.answer as the answer. The call reconnects after a dropped connection for up to 30 seconds. Corfedo AI is also a chat model for LangChain and Pydantic AI, and a chat client for Agent Framework. The conversation lives in Corfedo, so each call sends only the new message or the results of client tools:

from pydantic_ai import Agent
from corfedo.langchain import ChatCorfedo
from corfedo.pydantic_ai import CorfedoModel, CorfedoModelSettings

answer = await ChatCorfedo(client=client).ainvoke("How are failed payments retried?")
agent = Agent(CorfedoModel(client), model_settings=CorfedoModelSettings(corfedo_tools=["search_code"]))

In Agent Framework, use corfedo.agent_framework.CorfedoChatClient(client) as the agent's client. Runs in one agent session continue one conversation.

Messages are text. Sampling settings, system prompts and other content are rejected with CorfedoUnsupportedInputError. An administrator sets sampling and instructions in the Corfedo AI profile for API access.

More examples, including OpenAI Agents, Anthropic tool use and an Agent Framework session, are in examples/.

Documentation

Related repositories

Development

uv sync --locked --all-extras
uv run python scripts/check_contract.py
uv run python scripts/check_version.py
uv run ruff check && uv run ruff format --check
uv run pyright
uv run pytest
uv build
uv run python tests/package/check_wheel.py

python3 scripts/prepare_package.py builds a preview package for testing before a release. Its version comes from the sources, such as 0.0.1.dev0+s14275fd1c9c8e82a, so the same sources always give the same version. The wheel, the sdist and manifest.json go to artifacts/packages/<version>/. See CONTRIBUTING.md.

License

Apache-2.0, see LICENSE and NOTICE. The bundled API contract has its own notice in contracts/NOTICE. Corfedo names, logos and brand assets are reserved and not covered by these licenses.

About

Async Python client for Corfedo code search and Corfedo AI, with LangChain and Pydantic AI tools.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages