2023-11-15 08:20:30 +00:00
|
|
|
|
import json
|
2023-11-19 12:54:12 +00:00
|
|
|
|
|
|
|
|
|
import openai
|
|
|
|
|
from langchain.chains import LLMChain
|
|
|
|
|
from langchain.prompts import PromptTemplate
|
|
|
|
|
|
2023-11-18 15:08:22 +00:00
|
|
|
|
import proto.documents_pb2
|
2023-11-15 08:20:30 +00:00
|
|
|
|
from langchain import text_splitter
|
|
|
|
|
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
|
|
|
|
from langchain.embeddings import OpenAIEmbeddings
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# from langchain.llms.openai import OpenAI;
|
|
|
|
|
from langchain.chat_models.openai import ChatOpenAI
|
2023-11-15 08:20:30 +00:00
|
|
|
|
from langchain.schema.document import Document
|
|
|
|
|
from pymilvus import (
|
|
|
|
|
connections,
|
|
|
|
|
utility,
|
|
|
|
|
FieldSchema,
|
|
|
|
|
CollectionSchema,
|
|
|
|
|
DataType,
|
|
|
|
|
Collection,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
import init
|
|
|
|
|
import doc_client
|
2023-11-19 12:54:12 +00:00
|
|
|
|
import openai
|
|
|
|
|
from langchain.adapters import openai as lc_openai
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
2023-11-18 15:08:22 +00:00
|
|
|
|
#
|
|
|
|
|
# question = """
|
|
|
|
|
# yarn : File C:\\Users\\ivamp\\AppData\\Roaming\\npm\\yarn.ps1 cannot be loaded because running scripts is disabled on this sy
|
|
|
|
|
# stem. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
|
|
|
|
|
# At line:1 char:1
|
|
|
|
|
# + yarn config set registry https://registry.npm.taobao.org/
|
|
|
|
|
# + ~~~~
|
|
|
|
|
# + CategoryInfo : SecurityError: (:) [], PSSecurityException
|
|
|
|
|
# + FullyQualifiedErrorId : UnauthorizedAccess
|
|
|
|
|
#
|
|
|
|
|
# 是什么问题,该怎么解决
|
|
|
|
|
# """
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
question = """
|
2023-11-19 12:54:12 +00:00
|
|
|
|
错误 yarn 什么了遇到我
|
2023-11-15 08:20:30 +00:00
|
|
|
|
"""
|
2023-11-19 12:54:12 +00:00
|
|
|
|
#
|
|
|
|
|
# vec = init.text_to_vector(question)
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
vec = ""
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
with open("../question_vec.json", "r") as f:
|
|
|
|
|
vec = json.load(f)
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
search_param = {
|
|
|
|
|
"data": [vec],
|
|
|
|
|
"anns_field": "vector",
|
|
|
|
|
"param": {"metric_type": "L2"},
|
|
|
|
|
"limit": 10,
|
|
|
|
|
"expr": "user_id == 2",
|
|
|
|
|
"output_fields": ["todo_id", "title", "source", "todo_description", "language", "text", "user_id"],
|
|
|
|
|
}
|
|
|
|
|
res = init.collection.search(**search_param)
|
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# 保留 5 个
|
|
|
|
|
if len(res[0]) > 5:
|
|
|
|
|
res[0] = res[0][:5]
|
|
|
|
|
|
|
|
|
|
# document_chunk_ids = []
|
|
|
|
|
# real_document = []
|
|
|
|
|
plain_text = ""
|
2023-11-22 02:13:58 +00:00
|
|
|
|
document_chunks = []
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
for i in range(len(res[0])):
|
2023-11-18 15:08:22 +00:00
|
|
|
|
_chunk_id = res[0][i].id
|
|
|
|
|
print("正在获取分块 " + str(_chunk_id) + " 的内容...")
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2023-11-18 15:08:22 +00:00
|
|
|
|
_chunk_content = doc_client.stub.GetDocumentChunk(proto.documents_pb2.GetDocumentChunkByIdRequest(
|
|
|
|
|
id=_chunk_id
|
2023-11-15 08:20:30 +00:00
|
|
|
|
))
|
2023-11-18 15:08:22 +00:00
|
|
|
|
|
|
|
|
|
# print(_chunk_content)
|
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# _doc_content_full = _chunk_content.content
|
|
|
|
|
# print("DOC OBJ:" + _doc_content_full)
|
2023-11-22 02:13:58 +00:00
|
|
|
|
document_chunks.append(_chunk_content.content)
|
2023-11-19 12:54:12 +00:00
|
|
|
|
plain_text += "=== \n" + _chunk_content.content + " ===\n"
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
# real_document.append(_doc_content)
|
2023-11-18 15:08:22 +00:00
|
|
|
|
# doc_obj = Document(page_content=_doc_content_full, metadata={"source": _chunk_content.title})
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# doc_obj = Document(page_content=_doc_content_full, metadata={"source": "chunked content"})
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# real_document.append(doc_obj)
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# print(real_document)
|
2023-11-15 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
print("正在调用 LLM...")
|
2023-11-18 15:08:22 +00:00
|
|
|
|
|
2023-11-19 12:54:12 +00:00
|
|
|
|
# prompt_template = f"""Answer questions use the following context and reply in question spoken language and answer
|
|
|
|
|
# with Markdown format, you can fix syntax errors in the context, but do not change the meaning of the context.
|
|
|
|
|
# you can tell user context errors(syntax or meaning) in answer.
|
|
|
|
|
# ---
|
|
|
|
|
# {plain_text}
|
|
|
|
|
# ---
|
|
|
|
|
# Question: {question}
|
|
|
|
|
# Answer:"""
|
|
|
|
|
|
|
|
|
|
messages = [
|
|
|
|
|
{
|
|
|
|
|
"role": "system",
|
|
|
|
|
"content": """
|
|
|
|
|
回答问题使用以下上下文,并以提问的语言和Markdown回答,并告诉来源。
|
|
|
|
|
你得用“你”的身份指代用户。如果用户的问题有语法错误或者上下文的意思不对,你可以告诉用户。
|
|
|
|
|
请辨别上下文中的内容,有一些是不相干的。
|
|
|
|
|
"""
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "system",
|
|
|
|
|
"content": f"""
|
2023-11-22 02:13:58 +00:00
|
|
|
|
{plain_text}
|
2023-11-19 12:54:12 +00:00
|
|
|
|
"""
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": f"""
|
|
|
|
|
{question}
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
result = openai.ChatCompletion.create(
|
|
|
|
|
messages=messages, model="gpt-3.5-turbo", temperature=0
|
|
|
|
|
)
|
|
|
|
|
res = result["choices"][0]["message"].to_dict_recursive()
|
|
|
|
|
print(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# prompt_template = f"""
|
|
|
|
|
# ---
|
|
|
|
|
# {plain_text}
|
|
|
|
|
# ---
|
|
|
|
|
# Question: {question}
|
|
|
|
|
# Answer:"""
|
|
|
|
|
#
|
|
|
|
|
# print(prompt_template)
|
|
|
|
|
# # PROMPT = PromptTemplate(
|
|
|
|
|
# # template=prompt_template, input_variables=["real_document", "question"]
|
|
|
|
|
# # )
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# ChatOpenAI
|
|
|
|
|
|
|
|
|
|
# llm = OpenAI(temperature=0, model_name="gpt-3.5-turbo")
|
|
|
|
|
# # chain = LLMChain(llm=llm, prompt=PROMPT)
|
|
|
|
|
#
|
|
|
|
|
# output = llm(prompt_template)
|
|
|
|
|
|
|
|
|
|
# gpt = openai.Completion.create(
|
|
|
|
|
# engine="gpt-3.5-turbo",
|
|
|
|
|
# prompt=prompt_template,
|
|
|
|
|
# max_tokens=150,
|
|
|
|
|
# temperature=0,
|
|
|
|
|
# top_p=1,
|
|
|
|
|
# frequency_penalty=0,
|
|
|
|
|
# presence_penalty=0,
|
|
|
|
|
# stop=["==="]
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# output = gpt["choices"][0]["text"]
|
|
|
|
|
# print(output)
|
|
|
|
|
|
|
|
|
|
# output = chain({"real_document": real_document, "question": question}, return_only_outputs=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# print(output)
|
|
|
|
|
|
|
|
|
|
# chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type="map_reduce", return_intermediate_steps=True,
|
|
|
|
|
# verbose=True)
|
|
|
|
|
#
|
|
|
|
|
# question = "必须使用中文回复:" + question
|
|
|
|
|
# output = chain({"input_documents": real_document, "question": question}, return_only_outputs=False)
|
|
|
|
|
# print("回复:" + output["output_text"])
|