|
from langchain.embeddings.openai import OpenAIEmbeddings |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.vectorstores import FAISS |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.llms import OpenAI |
|
from gradio import gradio as gr |
|
from langchain.chat_models import ChatOpenAI |
|
|
|
from langchain.schema import AIMessage, HumanMessage |
|
from langchain import PromptTemplate, LLMChain |
|
from langchain.llms import TextGen |
|
|
|
|
|
|
|
import os |
|
OPENAI_API_KEY=os.getenv('OPENAI_API_KEY') |
|
|
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en") |
|
|
|
|
|
|
|
docsearch = FAISS.load_local("./bge-large-en_faiss_index/faiss_index", embeddings) |
|
|
|
|
|
chain = load_qa_chain(OpenAI(), chain_type="stuff",verbose=True) |
|
|
|
prompt = "您是回答所有ANSYS软件使用查询的得力助手,如果所问的内容不在范围内,请回答“您提的问题不在本知识库内,请重新提问”,所有问题必需用中文回答" |
|
|
|
def predict(message, history): |
|
history_openai_format = [] |
|
for human, assistant in history: |
|
history_openai_format.append({"role": "system", "content": prompt }) |
|
history_openai_format.append({"role": "user", "content": human }) |
|
history_openai_format.append({"role": "assistant", "content":assistant}) |
|
history_openai_format.append({"role": "user", "content": message}) |
|
|
|
response = openai.ChatCompletion.create( |
|
model='gpt-3.5-turbo', |
|
messages= history_openai_format, |
|
temperature=1.0, |
|
stream=True |
|
) |
|
|
|
partial_message = "" |
|
for chunk in response: |
|
if len(chunk['choices'][0]['delta']) != 0: |
|
partial_message = partial_message + chunk['choices'][0]['delta']['content'] |
|
yield partial_message |
|
|
|
gr.ChatInterface(predict, |
|
textbox=gr.Textbox(placeholder="请输入您的问题", container=False, scale=7), |
|
title="欢迎使用ANSYS软件AI机器人", |
|
examples=["你是谁?", "请介绍一下Fluent 软件的用户界面说明", "请用关于春天写一首100字的诗","数学题:小红有3元钱,小红买了2斤香蕉,香蕉的价格是每斤1元。问小红一共花了多少钱?","请用表格做一份学生课程表"], |
|
description="🦊请避免输入有违公序良俗的问题,模型可能无法回答不合适的问题🐇",).queue().launch() |