CoolFace
Apppublic

TangoDev/sam-test

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py122 linesDownload Raw Back to root
1import gradio as gr2import os 3from langchain.vectorstores import Chroma4from langchain.embeddings import CohereEmbeddings5from langchain.chat_models import ChatOpenAI6from langchain.chains import (7    StuffDocumentsChain, LLMChain8)9from langchain.schema import HumanMessage, AIMessage10from langchain.prompts import PromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder11from langchain.callbacks.manager import (12    trace_as_chain_group, 13)14 15 16OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')17COHERE_API_KEY = os.environ.get('COHERE_API_KEY')18 19### Set up our retriever20 21embeddings = CohereEmbeddings()22vectorstore = Chroma(embedding_function=embeddings, persist_directory="chroma")23retriever = vectorstore.as_retriever()24 25 26### Set up our chain that can answer questions based on documents27 28# This controls how each document will be formatted. Specifically,29# it will be passed to `format_document` - see that function for more30# details.31document_prompt = PromptTemplate(32    input_variables=["page_content"],33     template="{page_content}"34)35document_variable_name = "context"36llm = ChatOpenAI(temperature=0,model_name='gpt-4')37# The prompt here should take as an input variable the38# `document_variable_name`39prompt_template = """First, Rate the review and if the label is negative as ๐Ÿ‘Ž or positive as ๐Ÿ‘ or neutral as ๐Ÿ˜. Output as Rating:40On another line, provide only three keywords of the review. Output as Keywords:41On another line, determine if an Employee's name is used and if so please display otherwise display NO NAME GIVEN. Do not use the person's name in your response if you are uncertain if they are an employee of this organization.42On another line, determine what market it the review is for. For example, real estate, HVAC, roofing, etc. if you don't known display MARKET NOT KNOWN. Output as Market:43On another line, provide up to five more keywords based on services found in the review. Output as Services Keywords:44On another line, Analyze their writing style and tell me their education level and general age based on their review. Also list a % of confidence below your answer. 45Secondly, Write the response in the the education level and age level you found. Be genuine, sincere and helpful in your tone and response. Use the person's first name if known. Thank them if positive or apologize an be helpful if negative. Use all the service keywords you found in the response and bold with markdown. Response in a minimum of 500 characters and maximum of 4096 characters. Write it as a response to a customer review not a letter, do not start with dear person's first name and/or hello or any other salutation. Output as Response:46On another line, display the number of characters in the response. Output as Character count:47Finally, display this information in JSON in code. Output as JSON:48 49 50--------------51 52{context}"""53system_prompt = SystemMessagePromptTemplate.from_template(prompt_template)54prompt = ChatPromptTemplate(55	messages=[56		system_prompt, 57		HumanMessagePromptTemplate.from_template("{question}")58	]59)60llm_chain = LLMChain(llm=llm, prompt=prompt)61combine_docs_chain = StuffDocumentsChain(62    llm_chain=llm_chain,63    document_prompt=document_prompt,64    document_variable_name=document_variable_name,65    document_separator="---------"66)67 68### Set up a chain that controls how the search query for the vectorstore is generated69 70# This controls how the search query is generated.71# Should take `chat_history` and `question` as input variables.72template = """Combine the chat history and follow up question into a a search query.73 74Chat History:75 76{chat_history}77 78Follow up question: {question}79"""80prompt = PromptTemplate.from_template(template)81llm = ChatOpenAI(temperature=0)82question_generator_chain = LLMChain(llm=llm, prompt=prompt)83 84 85### Create our function to use86 87def qa_response(message, history):88 89	# Convert message history into format for the `question_generator_chain`.90	convo_string = "\n\n".join([f"Human: {h}\nAssistant: {a}" for h, a in history])91 92	# Convert message history into LangChain format for the final response chain.93	messages = []94	for human, ai in history:95		messages.append(HumanMessage(content=human))96		messages.append(AIMessage(content=ai))97 98	# Wrap all actual calls to chains in a trace group.99	with trace_as_chain_group("qa_response") as group_manager:100 101		# Generate search query.102		search_query = question_generator_chain.run(103			question=message, 104			chat_history=convo_string, 105			callbacks=group_manager106		)107 108		# Retrieve relevant docs.109		docs = retriever.get_relevant_documents(search_query, callbacks=group_manager)110 111		# Answer question.112		return combine_docs_chain.run(113			input_documents=docs, 114			chat_history=messages, 115			question=message, 116			callbacks=group_manager117		)118 119### Now we start the app!120 121gr.ChatInterface(qa_response).launch()122