CoffeeDemo / app.py
LeimingXu's picture
uncomment load_index_from_storage
b0c8c1b
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.utilities import SerpAPIWrapper
from langchain.chains import LLMChain
from langchain.chains import LLMRequestsChain
from langchain.prompts import PromptTemplate
from langchain.llms import AzureOpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.memory import ConversationBufferMemory
import gradio as gr
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from llama_index import ServiceContext, LLMPredictor, LangchainEmbedding
from llama_index.langchain_helpers.agents import IndexToolConfig, LlamaIndexTool
from pathlib import Path
import requests
import time
import openai, os
import openai
openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = os.getenv("OPENAI_API_VERSION")
openai.api_key = os.getenv("OPENAI_API_KEY")
import logging
from logging import handlers
from typing import Optional
def get_logger(name: str, file_name: Optional[str] = None) -> logging.Logger:
logger = logging.getLogger(name)
if file_name is None:
file_name = "app-default.log"
handler = handlers.TimedRotatingFileHandler(
filename=file_name, when="d", backupCount=21, encoding="UTF-8"
)
formatter = logging.Formatter("[%(asctime)s][%(levelname)s][%(message)s]")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Configure the logger as desired
# e.g., add handlers, set log levels, etc.
return logger
logger = get_logger("app", file_name="app.log")
openai_embedding = OpenAIEmbeddings(client=None, chunk_size=1)
embedding = LangchainEmbedding(openai_embedding)
llm = AzureOpenAI(
deployment_name="SBUXOpenAI",
# model_name="SBUXOpenAI",
model="gpt-3.5-turbo",
client=None,
# temperature set to 0.0(default 0.7) to get a certain answer from OpenAI,
# as a wiki robot we won't want to get flexible answers
temperature=0.0,
# GPT-3 default is 4096, however, openai.py default is 256
max_tokens=2048,
)
# define LLM
llm_predictor = LLMPredictor(llm=llm)
#index.save_to_disk('./data/FAQ/FAQ.json')
# Load the vector files from the directory
directory_path = "./data/FAQ"
service_context = ServiceContext.from_defaults(
llm_predictor=llm_predictor,
embed_model=embedding,
)
from llama_index import load_index_from_storage, StorageContext
# Create a storage context with the path to the index files
storage_context = StorageContext.from_defaults(persist_dir=directory_path)
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
from pathlib import Path
from llama_index import download_loader
SimpleCSVReader = download_loader("SimpleCSVReader")
loader = SimpleCSVReader()
documents = loader.load_data(file=Path('./data/FAQ.csv'))
#documents = SimpleDirectoryReader('./data').load_data()
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
index.storage_context.persist(persist_dir='./data/FAQ/FAQ.json')
print("[do_end][indexing] index is " + index.index_id)
print("[do_end][indexing] index is " + str(index.ref_doc_info))
# Load the index from the storage context
index = load_index_from_storage(storage_context, service_context=service_context)
print("[load_index_from_storage][indexing] index is " + index.index_id)
print("[load_index_from_storage][indexing] index is " + str(index.ref_doc_info))
'''
UnstructuredReader = download_loader("UnstructuredReader", refresh_cache=True)
loader = UnstructuredReader()
print("[do_start][reading]")
documents = loader.load_data(file=Path(f'./data.txt'), split_documents=False)
print("[do_end][reading]")
service_context = ServiceContext.from_defaults(
llm=llm,
embed_model=embedding,
)
print("[do_start][indexing]")
index = GPTVectorStoreIndex.from_documents(documents,service_context=service_context)
# print some log
print("[do_end][indexing]")
'''
memory = ConversationBufferMemory(memory_key="chat_history")
DEFAULT_QA_PROMPT_TMPL_PREFIX = (
"Given examples below.\n"
"---------------------\n"
)
DEFAULT_QA_PROMPT_TMPL_SUFFIX = (
"---------------------\n"
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"either say '不好意思,我从文档中无法找到答案' or answer the function: {query_str}\n"
)
print("[do_start][query_engine]")
from llama_index import Prompt
query_engine = index.as_query_engine(
service_context=service_context,
similarity_top_k=2,
text_qa_template=Prompt("\n".join([DEFAULT_QA_PROMPT_TMPL_PREFIX,
DEFAULT_QA_PROMPT_TMPL_SUFFIX])))
prefix = """Have a conversation with a human, thinking the following question step by step and answering it as best you can. Questions should be anwsered in the same language as the original question
You have access to the following tools, and these tools are not for translation tasks:"""
suffix = """Begin!"
{chat_history}
Question: {input}
{agent_scratchpad}"""
#agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True,memory=memory)
#print(agent.run("怎样MaxCloud中创建Bundle"))
def init():
print("[do_start][init]")
predict()
print("[do_end][init]")
def predict(input, history=[]):
print("[do_start][predict]")
failureTimes = 0
try:
print("input: " + input)
response = query_engine.query(input)
history.append((input,str(response)))
print("responses: " + str(response))
print("formated sources: " + response.get_formatted_sources())
return "",history
except Exception as e:
print("Exception is " + str(e))
failureTimes = failureTimes + 1
if failureTimes == 1:
orgInput = input
# input = "try to understand key concepts or entities in the \""+ orgInput + "\" firstly"
print("[do_end][predict]")
return "", history
print("[do_start][demo]")
with gr.Blocks(css="#chatbot{height:500px} .overflow-y-auto{height:500px}") as demo:
chatbot = gr.Chatbot(elem_id="chatbot")
state = gr.State([])
with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="欢迎一起探讨咖啡文化").style(container=False)
txt.submit(predict, [txt, state], [chatbot, state])
COFFEE_LANG_PROMPT = "请问戴帽卫衣可以穿吗?"
print(predict(COFFEE_LANG_PROMPT))
print("[do_end][demo]")
demo.launch(debug=True)