CodeIndex Python SDK - Direct SQLite database access
Project description
CodeIndex Python SDK
直接访问 CodeIndex SQLite 数据库的 Python SDK,无需 Node.js 运行时。
✨ 主要特性
- ✅ 无需 Node.js:直接读取 SQLite 数据库,无需启动 Node.js 进程
- ✅ 性能更好:无进程间通信开销,查询延迟从 50-100ms 降至 1-5ms
- ✅ 简单易用:API 清晰直观,保持向后兼容
- ✅ 类型提示:完整的类型定义,IDE 友好
- ✅ 轻量级:仅依赖 numpy,无其他重型依赖
📦 安装
从 PyPI 安装(推荐)
# 使用阿里云镜像源(推荐,速度更快)
pip install -i https://mirrors.aliyun.com/pypi/simple/ lydiacai-codeindex-sdk
# 或使用官方 PyPI 源
pip install lydiacai-codeindex-sdk
本地开发安装
cd sdk/python
pip install -e .
🚀 快速开始
基本使用
from codeindex_sdk import CodeIndexClient
# 方式1:直接使用数据库路径
with CodeIndexClient(".codeindex/project.db") as client:
# 查找符号
symbols = client.find_symbols(name="CreateUser", language="go")
for symbol in symbols:
print(f"{symbol['name']} at {symbol['location']['path']}:{symbol['location']['startLine']}")
# 查找单个符号
symbol = client.find_symbol("CreateUser", language="go")
if symbol:
print(f"Found: {symbol['qualifiedName']}")
# 查询对象属性
props = client.object_properties("UserService", language="go")
for prop in props:
print(f"{prop['kind']} {prop['name']}")
# 生成调用链
chain = client.call_chain(from_symbol=12345, direction="forward", depth=3)
if chain:
print(f"Call chain: {chain['name']} -> {len(chain.get('children', []))} calls")
# 获取定义位置
location = client.definition(12345)
if location:
print(f"Definition: {location['path']}:{location['startLine']}")
# 获取引用
refs = client.references(12345)
print(f"Found {len(refs)} references")
使用配置对象(向后兼容)
from codeindex_sdk import CodeIndexClient, CodeIndexConfig
# 方式2:使用配置对象(向后兼容)
config = CodeIndexConfig(
db_path=".codeindex/project.db",
# 以下参数已废弃,但保留用于兼容性
root_dir="/path/to/project",
languages=["go", "ts"],
)
with CodeIndexClient(config) as client:
symbols = client.find_symbols(name="CreateUser", language="go")
语义搜索(需要预先生成 embedding)
from codeindex_sdk import CodeIndexClient
import numpy as np
# 注意:需要先使用 CodeIndex CLI 生成 embedding
# 然后使用相同的 embedding 模型生成 query_embedding
with CodeIndexClient(".codeindex/project.db") as client:
# 假设你已经有了 query_embedding(例如使用 OpenAI API)
query_embedding = [0.1, 0.2, ...] # 你的 embedding 向量
results = client.semantic_search(
query="用户登录验证",
query_embedding=query_embedding,
model="text-embedding-3-small", # 与生成 embedding 时使用的模型一致
top_k=5,
language="go",
min_similarity=0.7
)
for result in results:
print(f"{result['symbol']['name']} (similarity: {result['similarity']:.2f})")
📚 API 文档
CodeIndexClient
__init__(db_path, **kwargs)
初始化客户端。
参数:
db_path(str | CodeIndexConfig): 数据库文件路径或配置对象node_command,worker_path,startup_timeout: 已废弃,忽略
find_symbols(name, language=None) -> List[Dict]
查找所有匹配名称的符号。
参数:
name(str): 符号名称language(str, optional): 语言过滤器
返回: 符号字典列表
find_symbol(name, language=None, in_file=None, kind=None) -> Dict | None
查找单个匹配条件的符号。
参数:
name(str): 符号名称language(str, optional): 语言过滤器in_file(str, optional): 文件路径过滤器kind(str, optional): 符号类型过滤器
返回: 符号字典或 None
object_properties(object_name, language=None) -> List[Dict]
获取对象/类/结构体的属性和方法。
参数:
object_name(str): 对象名称language(str, optional): 语言过滤器
返回: 属性字典列表
call_chain(from_symbol, direction="forward", depth=5) -> Dict | None
构建调用链。
参数:
from_symbol(int): 起始符号 IDdirection(str): "forward" 或 "backward"depth(int): 最大深度
返回: 调用链字典或 None
definition(symbol_id) -> Dict | None
获取符号的定义位置。
参数:
symbol_id(int): 符号 ID
返回: 位置字典或 None
references(symbol_id) -> List[Dict]
获取符号的所有引用。
参数:
symbol_id(int): 符号 ID
返回: 引用位置字典列表
semantic_search(query, query_embedding, model="default", top_k=10, ...) -> List[Dict]
语义搜索(需要预先生成 embedding)。
参数:
query(str): 查询文本(仅供参考)query_embedding(List[float]): 查询 embedding 向量(必需)model(str): Embedding 模型名称top_k(int): 返回结果数量language(str, optional): 语言过滤器kind(str, optional): 符号类型过滤器min_similarity(float): 最小相似度阈值
返回: 搜索结果列表(包含相似度分数)
⚙️ 工作原理
- 索引构建:使用 CodeIndex CLI(TypeScript)构建索引数据库
- 数据库查询:Python SDK 直接读取 SQLite 数据库
- 无需 Node.js:完全独立于 Node.js 运行时
📋 前置要求
- Python: >= 3.9
- 索引数据库:需要先使用 CodeIndex CLI 构建索引
node dist/src/cli/index.js index --root . --db .codeindex/project.db --lang go
🔄 从旧版本迁移
旧版本(0.1.x)
from codeindex_sdk import CodeIndexClient, CodeIndexConfig
config = CodeIndexConfig(
root_dir="/path/to/project",
db_path=".codeindex/project.db",
languages=["go", "ts"],
)
with CodeIndexClient(config) as client:
symbols = client.find_symbols(name="CreateUser", language="go")
新版本(0.2.x)
from codeindex_sdk import CodeIndexClient
# 简化:只需数据库路径
with CodeIndexClient(".codeindex/project.db") as client:
symbols = client.find_symbols(name="CreateUser", language="go")
注意:旧版本代码无需修改即可运行(向后兼容),但建议更新为新 API。
🆚 性能对比
| 指标 | 旧版本(Node Worker) | 新版本(直接 DB) | 提升 |
|---|---|---|---|
| 启动时间 | ~500ms | ~10ms | 50x |
| 查询延迟 | 50-100ms | 1-5ms | 20x |
| 内存占用 | Node + Python | 仅 Python | 减少 ~50MB |
| 依赖要求 | Node.js + Python | 仅 Python | 简化 |
🐛 常见问题
Q: 数据库文件不存在?
A: 请先使用 CodeIndex CLI 构建索引:
node dist/src/cli/index.js index --root . --db .codeindex/project.db --lang go
Q: 语义搜索返回空结果?
A: 确保:
- 已使用 CLI 生成 embedding:
node dist/src/cli/index.js embed - 使用相同的 embedding 模型生成 query_embedding
- 检查
min_similarity阈值是否过高
Q: 如何生成 query_embedding?
A: 使用你选择的 embedding API(如 OpenAI):
import openai
response = openai.embeddings.create(
model="text-embedding-3-small",
input="你的查询文本"
)
query_embedding = response.data[0].embedding
📄 许可证
MIT License
🔗 相关链接
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file caicai_codeindex-0.2.0.tar.gz.
File metadata
- Download URL: caicai_codeindex-0.2.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab9cb08563317087c6018675bd9c1818a3f6757db5a588d771f70f4a598ea1e1
|
|
| MD5 |
47ed69dca33f6bc1ae17e3b488db2e0b
|
|
| BLAKE2b-256 |
2d0abd42ecda5b56114801c83a6209e36b2e496ac57ceb9043d147410036f75c
|
File details
Details for the file caicai_codeindex-0.2.0-py3-none-any.whl.
File metadata
- Download URL: caicai_codeindex-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ece75a88891f00d8c2b6a7f0610efc15bf59d3eb22223105b7b3b58be3a47e4
|
|
| MD5 |
8a7e2329c9ea1d08d27ff6303fbe8734
|
|
| BLAKE2b-256 |
7cfad9393cc2a111f04bb6853e2afda9824bd605ce37d422be51d9e8c0358dc2
|