Spaces:
Sleeping
Sleeping
File size: 4,180 Bytes
79899c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# MCP 包装服务使用说明
## 概述
这个服务使用 `FastApiMCP` 将生物医学RAG服务包装成MCP(Model Context Protocol)工具,可以通过MCP客户端调用。
## 服务配置
在 `main.py` 中,服务被包装为:
```python
mcp = FastApiMCP(app, name="bio qa mcp", include_operations=["bio_qa_stream_chat"])
mcp.mount_sse()
```
## 可用的MCP操作
### 1. bio_qa_stream_chat
这是主要的生物医学问答操作,提供流式RAG问答服务。
## 调用方式
### 方式1: 通过MCP客户端调用
#### 1.1 配置MCP客户端
在你的MCP客户端配置中添加:
```json
{
"bio_qa_mcp": {
"url": "http://localhost:9487/sse",
"transport": "sse"
}
}
```
#### 1.2 调用示例
```python
# 使用MCP客户端调用
from langchain_mcp_adapters.client import MultiServerMCPClient
# 配置MCP服务器
mcp_config = {
"bio_qa_mcp": {
"url": "http://localhost:9487/sse",
"transport": "sse"
}
}
# 创建客户端
client = MultiServerMCPClient(mcp_config)
# 获取工具
tools = await client.get_tools()
# 使用工具
# 工具名称: bio_qa_stream_chat
# 参数: query (问题), lang (语言,可选,默认"en")
```
### 方式2: 直接HTTP调用
#### 2.1 直接调用API端点
```bash
# 调用生物医学问答接口
curl -X POST "http://localhost:9487/mcp/bio_qa" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "query=什么是糖尿病?&lang=zh"
```
#### 2.2 Python requests调用
```python
import requests
# 调用接口
response = requests.post(
"http://localhost:9487/mcp/bio_qa",
data={
"query": "什么是糖尿病?",
"lang": "zh"
}
)
# 处理流式响应
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
```
## 参数说明
### bio_qa_stream_chat 操作
- **query** (必需): 问题内容
- **lang** (可选): 语言设置
- `"zh"`: 中文
- `"en"`: 英文(默认)
## 响应格式
服务返回流式响应(Server-Sent Events),格式为:
```
data: {"type": "result", "content": "回答内容..."}
data: {"type": "result", "content": "更多内容..."}
data: {"type": "done", "content": "完成"}
```
## 使用场景
### 1. 在LangChain中使用
```python
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
# 创建代理
llm = ChatOpenAI(model="gpt-4")
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
# 执行问答
result = await agent_executor.ainvoke({
"input": "请帮我查询关于糖尿病的相关信息"
})
```
### 2. 在Streamlit应用中使用
```python
import streamlit as st
from langchain_mcp_adapters.client import MultiServerMCPClient
# 初始化MCP客户端
@st.cache_resource
def get_mcp_client():
config = {
"bio_qa_mcp": {
"url": "http://localhost:9487/sse",
"transport": "sse"
}
}
return MultiServerMCPClient(config)
# 使用
client = get_mcp_client()
tools = await client.get_tools()
```
## 部署说明
### 1. 启动服务
```bash
cd python-services/Retrieve
python main.py
```
服务将在 `http://localhost:9487` 启动。
### 2. 环境变量配置
确保设置了必要的环境变量:
```bash
export ENVIRONMENT=prod
export QA_LLM_MAIN_API_KEY=your-api-key
export QA_LLM_MAIN_BASE_URL=your-api-url
# ... 其他配置
```
### 3. 网络访问
- 本地访问: `http://localhost:9487`
- 远程访问: `http://your-server-ip:9487`
## 故障排除
### 常见问题
1. **连接失败**: 检查服务是否启动,端口是否正确
2. **认证错误**: 检查API密钥配置
3. **流式响应中断**: 检查网络连接稳定性
### 日志查看
服务会记录详细的日志信息,包括:
- 请求处理时间
- 错误信息
- 操作状态
## 扩展功能
### 添加新的MCP操作
1. 在 `routers/mcp_sensor.py` 中添加新的路由
2. 在 `main.py` 的 `include_operations` 中添加操作名称
3. 重新启动服务
### 自定义响应格式
可以修改 `ChatService` 来定制响应格式,满足不同的MCP客户端需求。 |