使用说明
MCP-模型-上下文-协议
使用LangChain和GPT-4展示模型上下文协议(MCP)的应用。此项目展示了如何构建具有上下文感知能力的AI应用,这些应用能够维护和利用对话上下文,以提供更加个性化和相关的响应。
什么是模型上下文协议(MCP)?
“模型上下文协议”是一种在语言模型交互中管理和利用上下文的方法。它实现了:
- 情境感知维护用户偏好、对话历史及相关背景信息
- 个性化回复根据积累的上下文定制AI回复
- 改进了连续性创建更连贯的多轮对话
特点/功能
✨(闪亮的星星或表示赞许、喜悦等的符号) 上下文感知的大型语言模型链展示如何使用LangChain在提示中注入上下文
🔑 翻译成中文是:钥匙 安全的API密钥管理使用 python-dotenv 来处理环境变量
🎯(目标) GPT-4 集成利用OpenAI的GPT-4模型进行智能回复
📝(这个符号本身没有直接的中文翻译,但通常表示“待办事项”或“笔记”的意思,可以理解为“📝 笔记/待办事项”) 实际示例包含一个完整的可运行示例,附有示例上下文和用户输入
安装
- 克隆仓库:
git clone https://github.com/chawla15/MCP-Model-Context-Protocol.git
cd MCP-Model-Context-Protocol
- 安装所需的依赖项:
pip install -r requirements.txt
- 创建一个
.env文件 在项目根目录下并添加您的OpenAI API密钥:
OPENAI_API_KEY=your_api_key_here
运行应用程序
运行主应用程序:
python main.py
这将执行MCP示例,展示在与GPT-4的对话中,上下文是如何被加载、管理和使用的。
代码示例
以下是核心实现代码 main.py:
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
# Load environment variables
load_dotenv()
def run_mcp_example():
"""Demonstrates Model Context Protocol with LangChain and GPT-4"""
# Initialize GPT-4 model
llm = ChatOpenAI(
model="gpt-4",
temperature=0.7,
api_key=os.getenv("OPENAI_API_KEY")
)
# Define conversation context
context = {
"user_name": "Alice",
"previous_topic": "artificial intelligence and machine learning",
"user_preference": "technical explanations with practical examples"
}
# Create context-aware prompt template
prompt_template = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI assistant. The user's name is {user_name}. "
"They previously discussed {previous_topic}. "
"They prefer {user_preference}."),
("user", "{user_input}")
])
# Build the LLM chain
chain = prompt_template | llm | StrOutputParser()
# Execute with context
response = chain.invoke({
"user_name": context["user_name"],
"previous_topic": context["previous_topic"],
"user_preference": context["user_preference"],
"user_input": "Can you explain how context is used in language models?"
})
return response
它是如何工作的
-
环境设置应用程序使用环境变量从OpenAI API加载API密钥
python-dotenv -
上下文定义用户特定的上下文定义包括:
- 用于个性化的用户名
- 保持连贯性的先前对话主题
- 用户对回复风格的偏好
-
提示工程A.
ChatPromptTemplate创建了一个机制,将上下文信息注入到系统消息中,确保人工智能了解用户的背景 -
大型语言模型(LLM)链创建LangChain的链语法(
|(运算符)连接:- 提示模板 → GPT-4模型 → 字符串输出解析器
-
上下文感知执行该链结合上下文和用户输入被调用,从而生成个性化的回复
示例输出
Initializing MCP Example with LangChain and GPT-4...
Context Information:
- User: Alice
- Previous Topic: artificial intelligence and machine learning
- Preference: technical explanations with practical examples
User Input: Can you explain how context is used in language models?
AI Response:
Hi Alice! Since you're interested in AI and ML, let me explain context in language models...
[Personalized response based on context]
MCP Example completed successfully!
项目结构
MCP-Model-Context-Protocol/
├── main.py # Main application with MCP implementation
├── requirements.txt # Project dependencies
├── README.md # Project documentation
└── .env # Environment variables (create this file)
依赖项
- LangChain基于语言模型的应用程序开发框架
- **LangChain-OpenAI(或可译为“OpenAI的LangChain集成/扩展”)**LangChain的OpenAI集成
- OpenAI官方OpenAI Python客户端库
- python-dotenv环境变量管理
用例
这个MCP实现可以扩展用于:
- 🤖 机器人 聊天机器人 记住用户偏好
- 📚 书籍 教育助理 适应学习风格的
- 💼 代表“商务/工作”或“文件夹”(根据上下文,这里更倾向于翻译为“商务/工作”以符合常用表达) 商业应用 维护客户上下文
- 🔍 翻译成中文是:🔍(这个符号本身没有直接的中文翻译,但可以理解为表示“查找”或“搜索”的意思,类似于中文里的“🔍”或“🔍”等表情符号所表达的概念) 研究工具 那个轨道调查主题
做出贡献
欢迎贡献!请随时提交拉取请求。
许可证
这个项目是开源的,并遵循MIT许可证发布。
