|
| 1 | +# Implementing OpenAPI-to-Function-Calling Architecture with KnowCode |
| 2 | + |
| 3 | +Integrating KnowCode to give AI agents intelligent codebase context involves translating KnowCode's REST API (FastAPI) into native "tools" or "functions" that the agent can autonomously call. |
| 4 | + |
| 5 | +The core idea is straightforward: **Convert the OpenAPI schema automatically generated by KnowCode into a list of tools that modern LLMs (OpenAI, Anthropic, Gemini) natively understand.** |
| 6 | + |
| 7 | +## 1. The Architecture Concept |
| 8 | + |
| 9 | +The architecture consists of three main components: |
| 10 | + |
| 11 | +1. **The KnowCode FastAPI Server**: Serves codebase intelligence endpoints and exposes its schema at `/openapi.json`. |
| 12 | +2. **The Translator layer**: Takes the `openapi.json` and parses it into JSON Schema-formatted function definitions. |
| 13 | +3. **The Agent Execution Loop**: The AI LLM decides to hit an endpoint (e.g., "I need context on the API handler"), and the execution loop makes the actual HTTP request to KnowCode and feeds the result back. |
| 14 | + |
| 15 | +```mermaid |
| 16 | +sequenceDiagram |
| 17 | + participant User |
| 18 | + participant Agent as AI Agent (e.g. GPT-4o) |
| 19 | + participant Intercept as Tool Translator / Executor |
| 20 | + participant KnowCode as KnowCode FastAPI Server |
| 21 | +
|
| 22 | + Note over KnowCode: 1. Server generates /openapi.json |
| 23 | + Intercept->>KnowCode: Fetch /openapi.json at startup |
| 24 | + Intercept-->>Agent: Pass endpoints as a list of "Tools" |
| 25 | +
|
| 26 | + User->>Agent: "Where is the search logic located?" |
| 27 | + Agent->>Agent: Identifies missing codebase context |
| 28 | + Agent->>Intercept: Action: Call function `query_context(query="search logic")` |
| 29 | + Intercept->>KnowCode: POST /api/v1/context/query |
| 30 | + KnowCode-->>Intercept: Returns matched code chunks |
| 31 | + Intercept-->>Agent: Returns Tool output (JSON context) |
| 32 | + Agent-->>User: "The search logic is located in `search_engine.py`..." |
| 33 | +``` |
| 34 | + |
| 35 | +## 2. Step-by-Step Implementation |
| 36 | + |
| 37 | +### Step 1: Start the KnowCode API Server |
| 38 | + |
| 39 | +KnowCode has a built-in FastAPI application (located in `src/knowcode/api/main.py`). When running, it automatically serves the OpenAPI standard schema. |
| 40 | + |
| 41 | +```bash |
| 42 | +# Start the KnowCode API server |
| 43 | +uvicorn knowcode.api.main:create_app --factory --port 8000 |
| 44 | +# The OpenAPI spec is now available at http://127.0.0.1:8000/openapi.json |
| 45 | +``` |
| 46 | + |
| 47 | +### Step 2: Translate OpenAPI into Agent Tools |
| 48 | + |
| 49 | +You map the valuable paths from the OpenAPI response into native LLM tool schemas. |
| 50 | + |
| 51 | +Here is an example structure in Python using the OpenAI SDK (this can be largely automated or done by frameworks like LangChain's `RequestsToolkit` or LlamaIndex's `OpenAPIToolSpec`): |
| 52 | + |
| 53 | +```python |
| 54 | +import requests |
| 55 | + |
| 56 | +# 1. Fetch KnowCode's schema |
| 57 | +openapi_spec = requests.get("http://127.0.0.1:8000/openapi.json").json() |
| 58 | + |
| 59 | +# 2. Extract specific API endpoints to provide as Functions/Tools |
| 60 | +tools = [ |
| 61 | + { |
| 62 | + "type": "function", |
| 63 | + "function": { |
| 64 | + "name": "query_context", |
| 65 | + "description": "Execute semantic search and return relevant code chunks with context. Use this when searching for vague concepts.", |
| 66 | + "parameters": { |
| 67 | + "type": "object", |
| 68 | + "properties": { |
| 69 | + "query": {"type": "string", "description": "The search query"}, |
| 70 | + "task_type": {"type": "string", "enum": ["explain", "debug", "extend", "review", "locate", "general"]} |
| 71 | + }, |
| 72 | + "required": ["query"] |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + { |
| 77 | + "type": "function", |
| 78 | + "function": { |
| 79 | + "name": "get_context", |
| 80 | + "description": "Generates a synthesized context bundle for a specific codebase entity (e.g. function or class).", |
| 81 | + "parameters": { |
| 82 | + "type": "object", |
| 83 | + "properties": { |
| 84 | + "target": {"type": "string", "description": "Entity ID or name to get context for"}, |
| 85 | + "max_tokens": {"type": "integer"} |
| 86 | + }, |
| 87 | + "required": ["target"] |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +] |
| 92 | + |
| 93 | +# 3. Supply tools to the AI Agent |
| 94 | +response = client.chat.completions.create( |
| 95 | + model="gpt-4o", |
| 96 | + messages=[{"role": "user", "content": "How does the caching system work?"}], |
| 97 | + tools=tools |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +### Step 3: Tool Execution Loop |
| 102 | + |
| 103 | +If the LLM responds with a `tool_calls` request, your application invokes the corresponding KnowCode HTTP endpoint: |
| 104 | + |
| 105 | +```python |
| 106 | +for tool_call in response.choices[0].message.tool_calls: |
| 107 | + if tool_call.function.name == "query_context": |
| 108 | + args = json.loads(tool_call.function.arguments) |
| 109 | + |
| 110 | + # Actually hit the KnowCode API |
| 111 | + api_res = requests.post( |
| 112 | + "http://127.0.0.1:8000/api/v1/context/query", |
| 113 | + json={"query": args["query"], "task_type": args.get("task_type", "general")} |
| 114 | + ) |
| 115 | + |
| 116 | + # Append the HTTP response back into standard LLM memory |
| 117 | + messages.append({ |
| 118 | + "role": "tool", |
| 119 | + "tool_call_id": tool_call.id, |
| 120 | + "content": api_res.text |
| 121 | + }) |
| 122 | +``` |
| 123 | + |
| 124 | +## 3. Highest Value KnowCode Endpoints for Agents |
| 125 | + |
| 126 | +When implementing this, you shouldn't expose every endpoint unconditionally to the agent. Based on `api.py`, the best endpoints to translate into Function Tools natively are: |
| 127 | + |
| 128 | +1. **`query_context`** (`POST /api/v1/context/query`): _Primary Discovery Tool._ Lets the agent search via natural language semantic search for topics it knows nothing about. |
| 129 | +2. **`search`** (`GET /api/v1/search`): _Exact Symbol Lookup._ When the agent wants to find the exact file/line of a known function or class name. |
| 130 | +3. **`get_context`** (`GET /api/v1/context`): _Deep Dive Tool._ Once the agent discovers an interesting Entity ID, it calls this to get a dense, token-capped context chunk tailored for LLM reasoning. |
| 131 | +4. **`trace_calls`** (`GET /api/v1/trace_calls/{entity_id}`): _Dependency Mapping._ When stepping through a debug process, the agent uses this to find callers and callees. |
0 commit comments