chore: rename multiple files to improve importability and module structure
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
from config import OPENAI_API_KEY
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
|
||||
# 定义提示词模板
|
||||
prompt = ChatPromptTemplate.from_messages([
|
||||
("system", "你非常可爱,说话末尾会带个喵"),
|
||||
("human", "{input}") # {input}:占位符
|
||||
])
|
||||
|
||||
# 初始化模型
|
||||
llm = ChatOpenAI(
|
||||
model="deepseek-chat",
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url="https://api.deepseek.com"
|
||||
)
|
||||
|
||||
# 定义解析器,把LLM返回的AIMessage转成字符串
|
||||
parser = StrOutputParser()
|
||||
|
||||
# 组成Chain
|
||||
chain = prompt | llm | parser
|
||||
|
||||
# 最终调用
|
||||
result = chain.invoke({"input": "你好喵"})
|
||||
print(result)
|
||||
@@ -0,0 +1,51 @@
|
||||
from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_community.chat_message_histories import ChatMessageHistory
|
||||
from langchain_core.runnables import RunnableWithMessageHistory
|
||||
from config import OPENAI_API_KEY
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages([
|
||||
("system", "你非常可爱,说话末尾会带个喵"),
|
||||
MessagesPlaceholder(variable_name="history"),
|
||||
("human", "{input}") # {input}:占位符
|
||||
])
|
||||
llm = ChatOpenAI(
|
||||
model="deepseek-chat",
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url="https://api.deepseek.com"
|
||||
)
|
||||
parser = StrOutputParser()
|
||||
|
||||
chain = prompt | llm | parser
|
||||
|
||||
# 存储所有会话历史(可用数据库替换)
|
||||
# 此处用字典模拟,也可替换成Redis、SQL等
|
||||
store = {}
|
||||
|
||||
def get_session_history(session_id:str):
|
||||
"""根据session_id获取该用户的聊天历史"""
|
||||
if session_id not in store:
|
||||
store[session_id] = ChatMessageHistory() # 创建新的历史记录
|
||||
return store[session_id]
|
||||
|
||||
|
||||
# 包装成带记忆的Runnable
|
||||
runnable_with_memory = RunnableWithMessageHistory(
|
||||
runnable=chain,
|
||||
get_session_history=get_session_history,
|
||||
input_messages_key="input",
|
||||
history_messages_key="history"
|
||||
)
|
||||
|
||||
session_id = 'user_123'
|
||||
while 1:
|
||||
user_input = input("\n你:")
|
||||
if user_input=="quit":
|
||||
print('拜拜喵!')
|
||||
break
|
||||
response = runnable_with_memory.invoke(
|
||||
{"input":user_input},
|
||||
config={"configurable":{"session_id":session_id}}
|
||||
)
|
||||
print(f'AI:{response}')
|
||||
@@ -0,0 +1,13 @@
|
||||
from config import OPENAI_API_KEY
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
# 初始化模型
|
||||
llm = ChatOpenAI(
|
||||
model="deepseek-chat",
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url="https://api.deepseek.com"
|
||||
)
|
||||
|
||||
# 调用模型
|
||||
response = llm.invoke('你好喵')
|
||||
print(response.content)
|
||||
@@ -0,0 +1,56 @@
|
||||
from config import OPENAI_API_KEY
|
||||
from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_community.chat_message_histories import ChatMessageHistory
|
||||
from langchain_core.runnables import RunnableWithMessageHistory
|
||||
|
||||
|
||||
def create_bot(llm,sys_prompt):
|
||||
"""
|
||||
:param sys_prompt: 系统提示词
|
||||
:param llm: 已配置好的语言模型实例
|
||||
:return:
|
||||
"""
|
||||
prompt = ChatPromptTemplate.from_messages([
|
||||
("system",sys_prompt),
|
||||
MessagesPlaceholder(variable_name="history"),
|
||||
("human","{input}")
|
||||
])
|
||||
parser = StrOutputParser()
|
||||
chain = prompt | llm | parser
|
||||
store = {}
|
||||
|
||||
def get_session_history(session_id):
|
||||
return store.setdefault(session_id,ChatMessageHistory())
|
||||
|
||||
return RunnableWithMessageHistory(
|
||||
runnable=chain,
|
||||
get_session_history=get_session_history,
|
||||
input_messages_key="input",
|
||||
history_messages_key="history"
|
||||
)
|
||||
|
||||
def main():
|
||||
# 此处集中配置LLM
|
||||
llm = ChatOpenAI(
|
||||
model="deepseek-chat",
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url="https://api.deepseek.com"
|
||||
)
|
||||
prompt = """你是‘小智’,一位专业、耐心且记忆力出色的 AI 助手。
|
||||
你善于倾听,能记住用户之前提到的信息,并在后续对话中自然提及。
|
||||
回答时简洁明了,避免冗余。"""
|
||||
bot = create_bot(llm,prompt)
|
||||
session_id = '123'
|
||||
while 1:
|
||||
user_input = input('\n你:')
|
||||
if user_input == "quit":
|
||||
print('拜拜')
|
||||
break
|
||||
response = bot.invoke({"input":user_input},config={"configurable":{"session_id":session_id}})
|
||||
print("AI:",response)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,10 @@
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
|
||||
# 定义提示词模板(推荐写法)
|
||||
prompt = ChatPromptTemplate.from_messages([
|
||||
("system","你非常可爱,说话末尾会带个喵"),
|
||||
("human","{input}") # {input}:占位符
|
||||
])
|
||||
# 格式化输出
|
||||
formatted_prompt = prompt.invoke({"input":"你好喵"})
|
||||
print(formatted_prompt)
|
||||
Reference in New Issue
Block a user