1. 背景
基于Azure OpenAI、Langchain构建企业内部知识库,当提出的问题,在企业内部知识库中不存在的时候,会回答企业知识库以外的内容,会给提问者造成信息的误解。回答的精准度也不高。
2. 具体实现代码如下
def ask_question(_temperature, _stream, _messages, _tenant_name, _collection_name, _verbose):
# 获取embedding 模型对象
embeddings = OpenAIEmbeddings(
deployment=_deployment,
model=_model,
chunk_size=1
)
# 获取pgvector 连接字符串
CONNECTION_STRING = PGVector.connection_string_from_db_params(
driver=_driver,
host=_host,
port=_port,
database=_database,
user=_user,
password=_password,
)
# 获取pgvector 模型对象
store = PGVector.connection_string_from_db_params(
driver=_driver,
host=_host,
port=_port,
database=_database,
user=_user,
password=_password,
)
# 获取chat 模型对象
retriever = store.as_retriever()
# 构造问答prompt
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:"""
prompt = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
llmAzureOpenAI = AzureChatOpenAI(
openai_api_type=_openai_api_type,
openai_api_base=_openai_api_base,
openai_api_version=_openai_api_version,
deployment_name=_deployment_name,
model_name=_model_name,
openai_api_key=_openai_api_key,
streaming=_stream,
temperature=_temperature,
verbose=_verbose
)
question_generator = LLMChain(llm=llmAzureOpenAI, prompt=CONDENSE_QUESTION_PROMPT)
doc_chain = load_qa_chain(llmAzureOpenAI, chain_type="stuff", prompt=prompt, verbose=_verbose)
chain = ConversationalRetrievalChain(
retriever=retriever,
combine_docs_chain=doc_chain,
question_generator=question_generator,
return_source_documents=True,
)
# 提高召回精读,默认0.8
vectordbkwargs = {"search_distance": 0.9}
chat_history = []
result = chain({'question': _messages, 'chat_history': chat_history, "vectordbkwargs": vectordbkwargs})
chat_history.append((_messages, result['answer']))
return result['answer']
3. 最后
我司的FinOps产品基于Azure OpenAI、PGVector提供企业私有智能助手解决方案。该解决方案包括私有知识库、私有智能客服、云专家和运维专家等功能,旨在为客户提供更快速、便捷的服务和支持。通过利用人工智能和自然语言处理技术,联蔚的FinOps产品能够帮助企业进行云消费的全生命周期管理,从而极大地节省云支出并提升效率。
如果有云上财务管理、企业私有智能助手的需求,可以关注我们的微信公众号、详情查看联蔚盘云官网。
相关链接:
联蔚FinOps平台全新升级:资源优化报告与华为云助您智慧管理多云环境
联蔚盘云成为Gartner《Cool Vendors in Cloud》报告中亚洲唯一云服务商
Azure OpenAI助力联蔚盘云企业智能助手全面上线,引领智能化转型新纪元