parent
2f9b99b50a
commit
3e1403fd94
@ -0,0 +1 @@
|
||||
API_KEY=9d2d36b2c8b20bb329fd44fe058b7ac2.UF14LzIdcAH2Ob21
|
@ -0,0 +1,31 @@
|
||||
import os
|
||||
|
||||
|
||||
class Property(object):
|
||||
__props = {}
|
||||
|
||||
filepath = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, ".config\\config.properties")
|
||||
with open(filepath, "r") as f:
|
||||
for line in f:
|
||||
l = line.strip()
|
||||
if l and not l.startswith('#'):
|
||||
key_map = l.split('=')
|
||||
key_name = key_map[0].strip()
|
||||
key_value = '='.join(key_map[1:]).strip().strip()
|
||||
__props[key_name] = key_value
|
||||
|
||||
@staticmethod
|
||||
def get_property(property_name):
|
||||
try:
|
||||
return Property.__props[property_name]
|
||||
except KeyError:
|
||||
print("there is no that property name")
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
print(Property.get_property("API_KEY"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,56 @@
|
||||
import os
|
||||
|
||||
from langchain_community.document_loaders import PyPDFLoader
|
||||
from langchain_core.callbacks import StreamingStdOutCallbackHandler, CallbackManager
|
||||
|
||||
from src.init.property import Property
|
||||
from langchain_community.chat_models.zhipuai import ChatZhipuAI
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
|
||||
|
||||
def format_docs(docs):
|
||||
return "\n\n".join(doc.page_content for doc in docs)
|
||||
|
||||
|
||||
class RAG:
|
||||
# 初始化ZHIPU API KEY
|
||||
os.environ["ZHIPUAI_API_KEY"] = Property.get_property("API_KEY")
|
||||
|
||||
# 初始化prompt工程
|
||||
__prompt = ChatPromptTemplate.from_messages([
|
||||
("system", "You are a world class technical documentation writer."),
|
||||
("user", "{input}")
|
||||
])
|
||||
|
||||
# 初始化模型
|
||||
__llm = ChatZhipuAI(
|
||||
temperature=0.95,
|
||||
model="glm-4"
|
||||
)
|
||||
|
||||
__streaming_chat = ChatZhipuAI(
|
||||
model="glm-4",
|
||||
temperature=0.5,
|
||||
streaming=True,
|
||||
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
|
||||
)
|
||||
|
||||
# 构建langchain
|
||||
chain = __prompt | __llm
|
||||
|
||||
def __init__(self, file_path: str):
|
||||
self.__file_path = file_path
|
||||
loader = PyPDFLoader(self.__file_path)
|
||||
file = loader.load()
|
||||
|
||||
def get_answer(self, message) -> str:
|
||||
return RAG.__llm.invoke(message).content
|
||||
|
||||
def get_streaming_chat(self, message) -> str:
|
||||
return RAG.__streaming_chat.invoke(message).content
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
r = RAG("C:\\Users\\16922\\Desktop\\文档1.pdf")
|
||||
print(r.get_streaming_chat("hello"))
|
||||
print(r.get_streaming_chat("what can you do"))
|
Loading…
Reference in new issue