Update app.py
Browse files408 unmodified lines4094104114124134145 unmodified lines42042142242342442542642742842943043143243343457 unmodified lines4924934944954964974984995005015025035045055065075085093 unmodified lines513514515516517518519520408 unmodified lineselse:return "No documents available to answer the query."def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {"Authorization": f"Bearer {API_TOKEN}",5 unmodified linesinstruction = f"""Using the following context from the PDF documents:{context}Write a detailed and complete response that answers the following user question: '{query}'"""else: # web searchinstruction = f"""Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""inputs = [{"role": "system", "content": instruction},{"role": "user", "content": query}]payload = {57 unmodified lines{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response.Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context.After writing the document, please provide a list of sources used in your response."""if model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):yield response, "" # Yield streaming response without sourceselse:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)main_content = ""for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],3 unmodified lines):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourcesINSTRUCTION_PROMPTS = {408 unmodified lines4094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434445 unmodified lines45045145245345445545645745845946046146246346446546646746857 unmodified lines5265275285295305315325335345355365375385395405415423 unmodified lines546547548549550551552553554555556408 unmodified lineselse:return "No documents available to answer the query."def validate_response(initial_response, context, query, model, temperature=0.1):validation_prompt = f"""Given the following context and initial response to the query "{query}":Context:{context}Initial Response:{initial_response}Please validate the initial response against the provided context. Remove any hallucinations, irrelevant, or factually incorrect information. Generate a revised response that is accurate and directly supported by the context. If any information cannot be verified from the context, state that explicitly.Revised Response:"""if model == "@cf/meta/llama-3.1-8b-instruct":return get_response_from_cloudflare(prompt=validation_prompt, context="", query="", num_calls=1, temperature=temperature, search_type="validation")else:client = InferenceClient(model, token=huggingface_token)revised_response = ""for message in client.chat_completion(messages=[{"role": "user", "content": validation_prompt}],max_tokens=10000,temperature=temperature,stream=True,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentrevised_response += chunkyield revised_responsedef get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {"Authorization": f"Bearer {API_TOKEN}",5 unmodified linesinstruction = f"""Using the following context from the PDF documents:{context}Write a detailed and complete response that answers the following user question: '{query}'"""elif search_type == "web":instruction = f"""Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""elif search_type == "validation":instruction = prompt # For validation, use the provided prompt directlyelse:raise ValueError("Invalid search_type")inputs = [{"role": "system", "content": instruction},{"role": "user", "content": query if search_type != "validation" else ""}]payload = {57 unmodified lines{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response.Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context."""initial_response = ""if model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):initial_response = responseelse:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],3 unmodified lines):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentinitial_response += chunk# Validation stepfor revised_response in validate_response(initial_response, context, query, model, temperature):yield revised_response, "" # Yield streaming revised response without sourcesINSTRUCTION_PROMPTS = {
app.py+44-8