result = qa({"question": question,"chat_history": chat_history})print(result["answer"])
chat_history.append((question, result["answer"]))
复制代码
2.实现一个带界面的RAG
未实际跑
# -*- coding: utf-8 -*-'''
llm和嵌入模型均调用Xinference接口
'''import streamlit as st
from langchain.llms import Xinference
from langchain.embeddings import XinferenceEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
# Customize the layout
st.set_page_config(page_title="Local AI Chat Powered by Xinference", page_icon=" ", layout="wide")# Write uploaded file in temp dirdefwrite_text_file(content, file_path):try:withopen(file_path,'w')asfile:file.write(content)returnTrueexcept Exception as e:print(f"Error occurred while writing the file: {e}")returnFalse# Prepare prompt template
prompt_template ="""
使用下面的上下文来回答问题。
如果你不知道答案,就说你不知道,不要编造答案。
{context}
问题: {question}
回答:"""
prompt = PromptTemplate(template=prompt_template, input_variables=["context","question"])# Initialize the Xinference LLM & Embeddings
prompt = ChatPromptTemplate.from_messages([("system","You are a Neo4j expert having a conversation about how to create Cypher queries",),("human","{input}"),])
input_variables=[‘question’, ‘samples’, ‘schema’] metadata={‘lc_hub_owner’: ‘ezhil’, ‘lc_hub_repo’: ‘text2cypher’, ‘lc_hub_commit_hash’: ‘0e15cdc2394c21084ae5e44d2632753bf4f83f2afa8427dec12c31e58f469449’} template="You are an expert Neo4j Cypher translator who understands the question in english and convert to Cypher strictly based on the Neo4j Schema provided and following the instructions below:\n1. Generate Cypher query compatible ONLY with Neo4j Version 5\n2. Do not use EXISTS, SIZE keywords in the cypher. Use alias when using the WITH keyword\n3. Please do not use same variable names for different nodes and relationships in the query.\n4. Use only Nodes and relationships mentioned in the schema\n5. Always enclose the Cypher output inside 3 backticks\n6. Always do a case-insensitive and fuzzy search for any properties related search. Eg: to search for a Company name use toLower(c.name) contains 'neo4j'\n8. Always use aliases to refer the node in the query\n9. ‘Answer’ is NOT a Cypher keyword. Answer should never be used in a query.\n10. Please generate only one Cypher query per question. \n11. Cypher is NOT SQL. So, do not mix and match the syntaxes.\n12. Every Cypher query always starts with a MATCH keyword.\n\nSchema:\n{schema}\nSamples:\n{samples}\n\nQuestion: {question}\nAnswer: "