Memic — Context Engineering SDK
What is Memic?
Memic is a managed context engineering platform. Instead of stuffing raw documents into your LLM's context window (expensive, slow, hits token limits), Memic handles the entire pipeline — document ingestion, chunking, embedding, vector storage — and gives you a single search API that returns only the relevant pieces. It also supports Text2SQL for structured databases, so one API covers both documents and databases.
The problem it solves: AI agents and LLM apps need grounded context from real data. Without Memic, you'd build and maintain your own chunking pipeline, embedding infrastructure, vector database, and query routing. Memic does all of this as a service — upload files, search with one API call, get back ranked chunks with source attribution.
Key capabilities:
- - Document search (RAG) — Upload PDFs, DOCX, PPTX, TXT, etc. Memic chunks, embeds, and indexes them. Search returns ranked content with file name, page number, and relevance score.
- Database search (Text2SQL) — Connect PostgreSQL/MySQL. Ask natural language questions, get SQL-generated results back.
- Hybrid search — Single API auto-routes queries to the right source (documents, database, or both).
- Multi-tenant isolation — Each API key is scoped to an org/project/environment. No data leaks between tenants.
- Metadata filters — Filter by reference ID, page range, category, document type.
Your API key auto-resolves all context (org, project, environment) — no IDs needed in API calls.
Coming soon:
- - MCP server — Native Model Context Protocol integration so AI agents (OpenClaw, Claude Code, etc.) can call Memic as a tool directly.
- Context compaction — Upload raw agent session logs or MEMORY.md files. Memic summarizes, compresses, and indexes them so agents can retrieve past context without re-loading full session history.
- Bulk context injection — Ingest entire knowledge bases (session JSONL, chat logs, wiki exports) in one call. Memic auto-chunks and indexes for instant search.
When to use this skill: Setting up Memic SDK, uploading documents, searching for context, building RAG pipelines, connecting databases for Text2SQL, debugging integration issues, or reducing LLM token costs by replacing raw context with targeted search.
Quick Start
CODEBLOCK0
CODEBLOCK1
First: Understand the Use Case
Ask the developer two questions:
Question 1: Integration Pattern
"How are you planning to use Memic?"
- 1. Context tool for an AI agent — Memic provides RAG context for an LLM agent (chatbot, copilot, assistant)
- Deterministic service — Direct search API in your app (no LLM involved)
Question 2: Data Source
"What type of data will you be searching?"
- 1. Unstructured (Documents) — PDFs, Word docs, text files → semantic vector search
- Structured (Databases) — PostgreSQL/MySQL → natural language to SQL
- Hybrid — Both document search and database queries via a single API
Prerequisites
- - Python 3.8+
- A Memic account at https://app.memic.ai
- An API key (starts with
mk_...)
Step 1: Get API Key
- 1. Go to https://app.memic.ai → Dashboard → API Keys
- Click "Create API Key"
- Copy the key
Important: Each API key is scoped to an organization + project + environment. The SDK auto-resolves this context — you never need to pass IDs.
Step 2: Install & Configure
CODEBLOCK2
CODEBLOCK3
Step 3: Verify Setup
CODEBLOCK4
If No Data Found
For documents: Upload via dashboard (https://app.memic.ai → Project → Upload) or SDK (see below).
For databases: Go to https://app.memic.ai → Connectors → Add Connector. Enter your PostgreSQL/MySQL connection details.
Core API Reference
Upload Files
CODEBLOCK5
Supported formats: PDF, DOCX, DOC, PPTX, XLSX, TXT, MD, HTML, and more.
Check File Status
CODEBLOCK6
Search Documents (Semantic)
CODEBLOCK7
Search with Metadata Filters
CODEBLOCK8
Available filters:
- -
reference_id / reference_ids — match file reference IDs - INLINECODE3 /
page_numbers — exact page match - INLINECODE5 — page range with
gte/ INLINECODE7 - INLINECODE8 — filter by category
- INLINECODE9 — filter by document type
Search with File Scoping
CODEBLOCK9
Hybrid Search (Documents + Databases)
When you have both documents and database connectors configured, Memic auto-routes queries:
CODEBLOCK10
Chat (RAG with built-in LLM)
CODEBLOCK11
Integration Patterns
Pattern A: Context Tool for AI Agent
Use Memic to inject grounded context into your LLM:
CODEBLOCK12
Pattern B: OpenClaw / AI Agent Context Optimization
Replace expensive raw-context loading with targeted search to cut token costs:
CODEBLOCK13
Pattern C: Deterministic Search API
Direct integration for app search functionality:
CODEBLOCK14
Debugging
| Issue | Solution |
|---|
| INLINECODE10 | Check MEMIC_API_KEY is set and valid |
| INLINECODE12 |
API key may be scoped to wrong project |
| Empty results | Check files are uploaded and status is
READY |
| Low scores | Lower
min_score or rephrase query |
| Timeout on upload | Large files take longer; increase
poll_timeout |
Exception Handling
CODEBLOCK15
Resources
- - SDK:
pip install memic | https://pypi.org/project/memic/ - Dashboard: https://app.memic.ai
- GitHub: https://github.com/memic-ai/memic-python
Memic — 上下文工程 SDK
什么是 Memic?
Memic 是一个托管式上下文工程平台。无需将原始文档直接塞入大语言模型的上下文窗口(成本高、速度慢、易触及 Token 限制),Memic 负责处理整个流程——文档导入、分块、嵌入、向量存储——并通过单一搜索 API 仅返回相关片段。它还支持针对结构化数据库的 Text2SQL 功能,因此一个 API 即可覆盖文档和数据库。
解决的问题:AI 智能体和 LLM 应用需要基于真实数据的可靠上下文。没有 Memic,你需要自行构建和维护分块管道、嵌入基础设施、向量数据库和查询路由。Memic 将这些作为服务提供——上传文件,通过一次 API 调用进行搜索,即可获得带来源归属的排序结果。
核心能力:
- - 文档搜索(RAG) — 上传 PDF、DOCX、PPTX、TXT 等文件。Memic 负责分块、嵌入和索引。搜索返回带文件名、页码和相关性分数的排序内容。
- 数据库搜索(Text2SQL) — 连接 PostgreSQL/MySQL。用自然语言提问,获取 SQL 生成的结果。
- 混合搜索 — 单一 API 自动将查询路由到正确的数据源(文档、数据库或两者)。
- 多租户隔离 — 每个 API 密钥限定于特定组织/项目/环境。租户间无数据泄露。
- 元数据过滤 — 按引用 ID、页码范围、类别、文档类型进行过滤。
你的 API 密钥会自动解析所有上下文(组织、项目、环境)——API 调用中无需传递 ID。
即将推出:
- - MCP 服务器 — 原生模型上下文协议集成,使 AI 智能体(OpenClaw、Claude Code 等)可直接将 Memic 作为工具调用。
- 上下文压缩 — 上传原始智能体会话日志或 MEMORY.md 文件。Memic 会进行总结、压缩和索引,使智能体无需重新加载完整会话历史即可检索过往上下文。
- 批量上下文注入 — 一次调用即可导入整个知识库(会话 JSONL、聊天日志、Wiki 导出)。Memic 自动分块和索引,实现即时搜索。
何时使用此技能:搭建 Memic SDK、上传文档、搜索上下文、构建 RAG 管道、连接数据库实现 Text2SQL、调试集成问题,或通过定向搜索替代原始上下文来降低 LLM Token 成本。
快速开始
bash
pip install memic
export MEMICAPIKEY=mkyourkey_here
python
from memic import Memic
client = Memic() # API 密钥自动解析组织/项目/环境
上传文档
file = client.upload_file(/path/to/doc.pdf)
搜索——仅返回相关片段,而非整个文档
results = client.search(query=关键发现是什么?, top_k=5)
for r in results:
print(f[{r.score:.2f}] {r.file
name} 第{r.pagenumber}页: {r.content[:100]})
首先:理解使用场景
向开发者提出两个问题:
问题 1:集成模式
你计划如何使用 Memic?
- 1. AI 智能体的上下文工具 — Memic 为 LLM 智能体(聊天机器人、Copilot、助手)提供 RAG 上下文
- 确定性服务 — 在你的应用中直接使用搜索 API(不涉及 LLM)
问题 2:数据源
你将搜索什么类型的数据?
- 1. 非结构化数据(文档) — PDF、Word 文档、文本文件 → 语义向量搜索
- 结构化数据(数据库) — PostgreSQL/MySQL → 自然语言转 SQL
- 混合模式 — 通过单一 API 同时进行文档搜索和数据库查询
前提条件
- - Python 3.8+
- 在 https://app.memic.ai 拥有 Memic 账户
- API 密钥(以 mk_... 开头)
步骤 1:获取 API 密钥
- 1. 访问 https://app.memic.ai → 仪表盘 → API 密钥
- 点击创建 API 密钥
- 复制密钥
重要提示:每个 API 密钥限定于特定组织 + 项目 + 环境。SDK 会自动解析此上下文——你无需传递 ID。
步骤 2:安装与配置
bash
pip install memic
bash
.env 文件
MEMIC
APIKEY=mk
yourapi
keyhere
步骤 3:验证设置
python
from memic import Memic
client = Memic()
检查 API 密钥解析结果
print(f组织: {client.org_id})
print(f项目: {client.project_id})
print(f环境: {client.environment_slug})
列出组织中的项目
projects = client.list_projects()
for p in projects:
print(f - {p.name} ({p.id}))
如果未找到数据
对于文档: 通过仪表盘(https://app.memic.ai → 项目 → 上传)或 SDK(见下文)上传。
对于数据库: 访问 https://app.memic.ai → 连接器 → 添加连接器。输入你的 PostgreSQL/MySQL 连接详情。
核心 API 参考
上传文件
python
上传并等待处理完成
file = client.upload_file(
file_path=/path/to/document.pdf,
reference
id=lesson123, # 可选——用于外部系统关联
metadata={category: legal}, # 可选——自定义键值对
)
print(fID: {file.id}, 状态: {file.status}) # 完成后 status = ready
支持格式:PDF、DOCX、DOC、PPTX、XLSX、TXT、MD、HTML 等。
检查文件状态
python
file = client.getfilestatus(file_id=...)
print(f状态: {file.status})
print(f处理中: {file.status.is_processing})
print(f失败: {file.status.is_failed})
print(f分块数: {file.total_chunks})
搜索文档(语义搜索)
python
results = client.search(
query=关键发现是什么?,
top_k=10,
min_score=0.7,
)
print(f找到 {results.totalresults} 个结果,耗时 {results.searchtime_ms} 毫秒)
for r in results:
print(f[{r.score:.2f}] {r.filename} 第{r.pagenumber}页: {r.content[:150]})
带元数据过滤的搜索
python
from memic import MetadataFilters, PageRange
results = client.search(
query=合同条款,
top_k=5,
filters=MetadataFilters(
referenceid=contract2024, # 按引用过滤
page_range=PageRange(gte=1, lte=20), # 仅第 1-20 页
category=legal, # 按类别
)
)
可用过滤器:
- - referenceid / referenceids — 匹配文件引用 ID
- pagenumber / pagenumbers — 精确页码匹配
- pagerange — 页码范围,支持 gte/lte
- category — 按类别过滤
- documenttype — 按文档类型过滤
限定文件范围的搜索
python
仅在特定文件中搜索
results = client.search(
query=收入数据,
file_ids=[file-id-1, file-id-2],
top_k=5,
)
混合搜索(文档 + 数据库)
当同时配置了文档和数据库连接器时,Memic 会自动路由查询:
python
results = client.search(query=按收入显示前十大客户)
检查查询路由方式
if results.routing:
print(f路由: {results.routing.route}) # semantic、structured 或 hybrid
print(f原因: {results.routing.reasoning})
文档结果
if results.has_documents:
for r in results.results.semantic:
print(f[文档] {r.file_name}: {r.content[:100]})
数据库结果(Text2SQL)
if results.has_structured:
print(fSQL: {results.routing.sql_generated})
for row in results.results.structured.rows:
print(f[数据库] {row})
聊天(带内置 LLM 的 RAG)
python
response = client._request(
POST, /sdk/chat,
json={question: 第四季度业绩如何?, topk: 5, minscore: 0.5}
)
print(response[answer])
print(f引用来源: {response[citations]})
print(f模型: {response[model]})
集成模式
模式 A:AI 智能体的上下文工具