fastapi-agent


Namefastapi-agent JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryAdd an AI Agent to your FastAPI application. The agent knows how to interact with your endpoints within a chat interface.
upload_time2025-08-04 10:23:41
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords agent ai api chat fastapi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ๐Ÿค– FastAPI Agent

> ๐Ÿ’ฌ **Talk to your FastAPI app like it's a teammate.**

FastAPI Agent integrates an AI Agent into your FastAPI application.  
It allows interaction with your API endpoints through a chat interface or directly via the `/agent/query` API route.

## โš™๏ธ Installation:

To install the package, run:
```bash
pip install fastapi_agent
```


## ๐Ÿงช Usage:

To use the FastAPI Agent, initialize it with your FastAPI app and AI model.<br>
You can use the default agent routes or add custom ones to your FastAPI application to interact with the agent via a chat interface or API endpoint.

Here is a simple example of how to use the FastAPI Agent with your FastAPI application:

#### .env
```bash
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

#### app.py
```python
import uvicorn
from dotenv import load_dotenv

from fastapi import FastAPI
from fastapi_agent import FastAPIAgent

# load OPENAI_API_KEY from .env
load_dotenv()

# set your FastAPI app
app = FastAPI(
    title="YOUR APP TITLE",
    version="0.1.0",
    description="some app description",
)

# add routes
@app.get("/")
async def root():
    """Welcome endpoint that returns basic API information"""
    return {"message": "Welcome to Test API"}

# add the FastAPI Agent + default routes
FastAPIAgent(
    app,
    model="openai:gpt-4",
    base_url="http://localhost:8000",
    include_router=True,
)

uvicorn.run(app, host="0.0.0.0", port=8000)
```


## ๐Ÿงญ Default Routes

FastAPI Agent provides two default routes:

1. **`/agent/query`** โ€“ Ask anything about your API using natural language. ๐Ÿง 

  ```bash
curl -k -X POST "http://127.0.0.1:8000/agent/query" \
  -H "Content-Type: application/json" \
  -d '{"query": "show all endpoints"}'
```

2. **`/agent/chat`** โ€“ A simple web-based chat interface to interact with your API. ๐Ÿ’ฌ

<br>

> ๐Ÿ’ก _You can also add custom routes using agent.chat() methode - [Example](https://github.com/orco82/fastapi-agent/blob/main/examples/3_fastapi_agent_example.py)_
 

## ๐Ÿงฉ Additional Arguments:
If your application routes use **Depends** (e.g., an API key), you can pass a dictionary of headers.  
The agent will use them to call your routes and apply the same dependencies to the `/agent/query` route.

```python
api_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

FastAPIAgent(
    app,
    model="openai:gpt-4",
    base_url="https://localhost:8000",
    deps={"api-key": api_key},
    include_router=True,
)
```

---

You can also pass the `ignore_routes` argument to prevent the agent from accessing specific routes in your application:

```python

FastAPIAgent(
    app,
    model="openai:gpt-4",
    base_url="https://localhost:8000",
    ignore_routes=["/user/delete/{user_id}"],
    include_router=True,
)

```

## ๐Ÿ“ Additional Examples:

Check out our examples for [ai_agent](https://github.com/orco82/fastapi-agent/blob/main/examples/1_ai_agent_example.py), 
[fastapi_discovery](https://github.com/orco82/fastapi-agent/blob/main/examples/2_fastapi_discovery_example.py), 
and [fastapi_agent](https://github.com/orco82/fastapi-agent/blob/main/examples/3_fastapi_agent_example.py).  
All examples are available [here](https://github.com/orco82/fastapi-agent/blob/main/examples/).

---

#### If you're using *Depends* in your routes, make sure to pass the required headers when calling the `/agent/query` endpoint like the examples below:

#### python
```python
import requests

res = requests.post(
    "http://127.0.0.1:8000/agent/query", 
    json={"query": "show all endpoints"},
    headers={"deps": '{"api-key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}'}
)
print(res.json())
```

#### curl
```bash
curl -k -X POST "http://127.0.0.1:8000/agent/query" \
  -H 'deps: {"api-key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}' \
  -H "Content-Type: application/json" \
  -d '{"query": "show all endpoints"}'
```

## ๐Ÿ“œ License

This project is licensed under the MIT License.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-agent",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "agent, ai, api, chat, fastapi",
    "author": null,
    "author_email": "Or Cohen <orco82@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b3/e6/fa06632404978196f3164fbcfa8608dd9b62df1795e1b800185ea97c3da2/fastapi_agent-0.1.4.tar.gz",
    "platform": null,
    "description": "# \ud83e\udd16 FastAPI Agent\n\n> \ud83d\udcac **Talk to your FastAPI app like it's a teammate.**\n\nFastAPI Agent integrates an AI Agent into your FastAPI application.  \nIt allows interaction with your API endpoints through a chat interface or directly via the `/agent/query` API route.\n\n## \u2699\ufe0f Installation:\n\nTo install the package, run:\n```bash\npip install fastapi_agent\n```\n\n\n## \ud83e\uddea Usage:\n\nTo use the FastAPI Agent, initialize it with your FastAPI app and AI model.<br>\nYou can use the default agent routes or add custom ones to your FastAPI application to interact with the agent via a chat interface or API endpoint.\n\nHere is a simple example of how to use the FastAPI Agent with your FastAPI application:\n\n#### .env\n```bash\nOPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n```\n\n#### app.py\n```python\nimport uvicorn\nfrom dotenv import load_dotenv\n\nfrom fastapi import FastAPI\nfrom fastapi_agent import FastAPIAgent\n\n# load OPENAI_API_KEY from .env\nload_dotenv()\n\n# set your FastAPI app\napp = FastAPI(\n    title=\"YOUR APP TITLE\",\n    version=\"0.1.0\",\n    description=\"some app description\",\n)\n\n# add routes\n@app.get(\"/\")\nasync def root():\n    \"\"\"Welcome endpoint that returns basic API information\"\"\"\n    return {\"message\": \"Welcome to Test API\"}\n\n# add the FastAPI Agent + default routes\nFastAPIAgent(\n    app,\n    model=\"openai:gpt-4\",\n    base_url=\"http://localhost:8000\",\n    include_router=True,\n)\n\nuvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\n\n## \ud83e\udded Default Routes\n\nFastAPI Agent provides two default routes:\n\n1. **`/agent/query`** \u2013 Ask anything about your API using natural language. \ud83e\udde0\n\n  ```bash\ncurl -k -X POST \"http://127.0.0.1:8000/agent/query\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"query\": \"show all endpoints\"}'\n```\n\n2. **`/agent/chat`** \u2013 A simple web-based chat interface to interact with your API. \ud83d\udcac\n\n<br>\n\n> \ud83d\udca1 _You can also add custom routes using agent.chat() methode - [Example](https://github.com/orco82/fastapi-agent/blob/main/examples/3_fastapi_agent_example.py)_\n \n\n## \ud83e\udde9 Additional Arguments:\nIf your application routes use **Depends** (e.g., an API key), you can pass a dictionary of headers.  \nThe agent will use them to call your routes and apply the same dependencies to the `/agent/query` route.\n\n```python\napi_key = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"\n\nFastAPIAgent(\n    app,\n    model=\"openai:gpt-4\",\n    base_url=\"https://localhost:8000\",\n    deps={\"api-key\": api_key},\n    include_router=True,\n)\n```\n\n---\n\nYou can also pass the `ignore_routes` argument to prevent the agent from accessing specific routes in your application:\n\n```python\n\nFastAPIAgent(\n    app,\n    model=\"openai:gpt-4\",\n    base_url=\"https://localhost:8000\",\n    ignore_routes=[\"/user/delete/{user_id}\"],\n    include_router=True,\n)\n\n```\n\n## \ud83d\udcc1 Additional Examples:\n\nCheck out our examples for [ai_agent](https://github.com/orco82/fastapi-agent/blob/main/examples/1_ai_agent_example.py), \n[fastapi_discovery](https://github.com/orco82/fastapi-agent/blob/main/examples/2_fastapi_discovery_example.py), \nand [fastapi_agent](https://github.com/orco82/fastapi-agent/blob/main/examples/3_fastapi_agent_example.py).  \nAll examples are available [here](https://github.com/orco82/fastapi-agent/blob/main/examples/).\n\n---\n\n#### If you're using *Depends* in your routes, make sure to pass the required headers when calling the `/agent/query` endpoint like the examples below:\n\n#### python\n```python\nimport requests\n\nres = requests.post(\n    \"http://127.0.0.1:8000/agent/query\", \n    json={\"query\": \"show all endpoints\"},\n    headers={\"deps\": '{\"api-key\": \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"}'}\n)\nprint(res.json())\n```\n\n#### curl\n```bash\ncurl -k -X POST \"http://127.0.0.1:8000/agent/query\" \\\n  -H 'deps: {\"api-key\": \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"}' \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"query\": \"show all endpoints\"}'\n```\n\n## \ud83d\udcdc License\n\nThis project is licensed under the MIT License.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Add an AI Agent to your FastAPI application. The agent knows how to interact with your endpoints within a chat interface.",
    "version": "0.1.4",
    "project_urls": {
        "Bug Tracker": "https://github.com/orco82/fastapi-agent/issues",
        "Documentation": "https://github.com/orco82/fastapi-agent#readme",
        "Homepage": "https://github.com/orco82/fastapi-agent",
        "Repository": "https://github.com/orco82/fastapi-agent"
    },
    "split_keywords": [
        "agent",
        " ai",
        " api",
        " chat",
        " fastapi"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f75b2fe757342d089aa663e8d46b0e54049ded623094d7a6b36190126160738e",
                "md5": "16a767d0664090df3fb1f891cd48c36e",
                "sha256": "893bc4639901eb2a66e71d1b54107f9e89922c24c597abad17b45ba40a39d493"
            },
            "downloads": -1,
            "filename": "fastapi_agent-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16a767d0664090df3fb1f891cd48c36e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 25998,
            "upload_time": "2025-08-04T10:23:39",
            "upload_time_iso_8601": "2025-08-04T10:23:39.635484Z",
            "url": "https://files.pythonhosted.org/packages/f7/5b/2fe757342d089aa663e8d46b0e54049ded623094d7a6b36190126160738e/fastapi_agent-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3e6fa06632404978196f3164fbcfa8608dd9b62df1795e1b800185ea97c3da2",
                "md5": "8f2d46955f46dc254c7da107f753a6bb",
                "sha256": "df69883d5b279b374244f3ea84b064f984ac7d0dc9f82ad71846fe76aabc9b17"
            },
            "downloads": -1,
            "filename": "fastapi_agent-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "8f2d46955f46dc254c7da107f753a6bb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 121446,
            "upload_time": "2025-08-04T10:23:41",
            "upload_time_iso_8601": "2025-08-04T10:23:41.255356Z",
            "url": "https://files.pythonhosted.org/packages/b3/e6/fa06632404978196f3164fbcfa8608dd9b62df1795e1b800185ea97c3da2/fastapi_agent-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-04 10:23:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "orco82",
    "github_project": "fastapi-agent",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "fastapi-agent"
}
        
Elapsed time: 1.13590s