上下文增强工具

基于FastAPI的模型上下文协议(MCP)服务器,可将简单文本上下文增强为详细的提示,并提供缓存功能。

  • by GoomeGum
  • Jupyter Notebook
  • 本地部署
  • 更新于
在 GitHub 查看 goomegum/mcp-server

使用说明

MCP服务器-上下文增强工具

一个基于FastAPI的模型上下文协议(MCP)服务器,它将简单的文本上下文增强为具有缓存功能的详细提示。 为Google Colab开发,支持GPU。

特性

  • 上下文细化工具:将简单的文本上下文转换为详细、精细的提示
  • 缓存提示:通过概念ID访问预先生成的详细提示
  • 快速缓存管理:列出所有缓存的详细提示
  • CSV数据加载:从CSV文件加载预先训练好的详细提示

项目结构

mcp_server/
├── main.py                      # FastAPI application with MCP endpoints
├── models.py                    # Pydantic models for request/response schemas
├── cache.py                     # Caching functionality and prompt elaboration
├── requirements.txt             # Python dependencies
├── similarity_results_train.csv # Training data for cached elaborate prompts
├── MCPServer.ipynb              # Google Colab notebook implementation
└── README.md                    # This file

API终点

工具

  • POST /tool/get_elaborate_description_prompt -将简单的上下文转换为详细的提示

资源

  • GET /resource/cached_description/{concept_id} -按ID获取缓存的详细提示
  • GET /resource/cached_description_list -列出所有缓存的详细提示

快速开始

Google Colab(推荐)

  1. 打开 MCPServer.ipynb 谷歌Colab中的笔记本
  2. 运行所有单元格以安装依赖项并启动服务器
  3. 服务器将创建一个公共ngrok隧道-访问API,网址: {ngrok_url}/docs

本地开发(需要GPU)

  1. 安装依赖项:

    pip install -r requirements.txt
    
  2. 访问API文档: http://localhost:8000/docs

使用示例

生成详细提示

curl -X POST "http://localhost:8000/tool/get_elaborate_description_prompt" \
     -H "Content-Type: application/json" \
     -d '{"context": "a sunset over mountains"}'

获取缓存的详细提示

curl "http://localhost:8000/resource/cached_description/concept123"

列出所有缓存的详细提示

curl "http://localhost:8000/resource/cached_description_list"

数据格式

服务器从以下位置加载缓存的详细提示 similarity_results_train.csv 格式如下:

  • input:简单概念/上下文标识符
  • finetuned_model_answer:精心制定的强化提示

发展

该服务器旨在与Phi-4或类似的语言模型配合使用,将简单的文本上下文转换为复杂、详细的提示。 该项目针对Google Colab进行了优化 GPU支持高效运行微调模型。

Colab的主要功能:

  • ngrok整合:创建用于从外部访问服务器的公共隧道
  • GPU支持:利用Colab的免费GPU进行模型推理
  • 预配置设置:笔记本中处理的所有依赖关系和模型加载
  • 方便访问:服务器文档可在 {ngrok_url}/docs 运行后

地方发展说明:

当前实现包括占位符逻辑 cache.py 当在没有GPU访问的情况下本地运行时,可以用实际的模型调用来替换,以增强上下文并提示细化。