|
|
''' |
|
|
Copyright 2024-2025 Infosys Ltd. |
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
''' |
|
|
|
|
|
from langchain_core.output_parsers import StrOutputParser |
|
|
from langchain_core.prompts import PromptTemplate |
|
|
from langchain_core.runnables import RunnablePassthrough |
|
|
from langchain_community.chat_models import AzureChatOpenAI |
|
|
import openai |
|
|
import os |
|
|
import time |
|
|
from config.logger import CustomLogger |
|
|
log = CustomLogger() |
|
|
|
|
|
|
|
|
class Cov: |
|
|
def cov(text,complexity, model_name): |
|
|
try: |
|
|
if model_name == "gpt3": |
|
|
deployment_name = os.getenv("OPENAI_MODEL_GPT3") |
|
|
azure_endpoint = os.environ.get("OPENAI_API_BASE_GPT3") |
|
|
openai_api_key = os.environ.get("OPENAI_API_KEY_GPT3") |
|
|
openai_api_version = os.environ.get("OPENAI_API_VERSION_GPT3") |
|
|
else: |
|
|
deployment_name = os.getenv("OPENAI_MODEL_GPT4") |
|
|
azure_endpoint = os.environ.get("OPENAI_API_BASE_GPT4") |
|
|
openai_api_key = os.environ.get("OPENAI_API_KEY_GPT4") |
|
|
openai_api_version = os.environ.get("OPENAI_API_VERSION_GPT4") |
|
|
|
|
|
|
|
|
|
|
|
openai_api_type = os.environ.get("OPENAI_API_TYPE") |
|
|
|
|
|
except Exception as e: |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
try: |
|
|
llm_1 = AzureChatOpenAI(model=deployment_name,openai_api_version=openai_api_version,openai_api_key=openai_api_key,azure_endpoint=azure_endpoint,openai_api_type ='azure',temperature = 0) |
|
|
llm_2 = AzureChatOpenAI(model=deployment_name, openai_api_version=openai_api_version, openai_api_key=openai_api_key, azure_endpoint=azure_endpoint,openai_api_type ='azure',temperature = 0.7) |
|
|
llm_3 = AzureChatOpenAI(model=deployment_name, openai_api_version=openai_api_version, openai_api_key=openai_api_key, azure_endpoint=azure_endpoint,openai_api_type ='azure',temperature = 2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
BASELINE_PROMPT_LONG = """Answer the below question correctly. |
|
|
Question: {original_question} |
|
|
Answer:""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VERIFICATION_QUESTION_PROMPT_LONG = """Your task is to create verification questions based on the below original question and the baseline response. The verification questions are meant for verifying the factual acuracy in the baseline response. Output should be numbered list of verification questions.Always come up with 5 questions. |
|
|
Actual Question: {original_question} |
|
|
Baseline Response: {baseline_response} |
|
|
Final Verification Questions:""" |
|
|
|
|
|
VERIFICATION_QUESTION_PROMPT_LONG_simple = """Your task is to create verification questions based on the below original question and the baseline response and the question should be very simple. The verification questions are meant for verifying the factual acuracy in the baseline response. Output should be numbered list of verification questions.Always come up with 5 questions. |
|
|
Actual Question: {original_question} |
|
|
Baseline Response: {baseline_response} |
|
|
Final Verification Questions:""" |
|
|
|
|
|
VERIFICATION_QUESTION_PROMPT_LONG_medium = """Your task is to create verification questions based on the below original question and the baseline response and the question should be moderate neither complex nor simple. The verification questions are meant for verifying the factual acuracy in the baseline response. Output should be numbered list of verification questions. |
|
|
Actual Question: {original_question} |
|
|
Baseline Response: {baseline_response} |
|
|
Final Verification Questions:""" |
|
|
|
|
|
VERIFICATION_QUESTION_PROMPT_LONG_complex = """Your task is to create verification questions based on the below original question and the baseline response and the question should be more complex not a simple question. The verification questions are meant for verifying the factual acuracy in the baseline response. Output should be numbered list of verification questions.Always come up with 5 questions. |
|
|
Actual Question: {original_question} |
|
|
Baseline Response: {baseline_response} |
|
|
Final Verification Questions:""" |
|
|
|
|
|
EXECUTE_PLAN_PROMPT_SELF_LLM = """Answer the following question correctly. |
|
|
Question: {verification_question} |
|
|
Answer:""" |
|
|
|
|
|
FINAL_REFINED_PROMPT = """Given the below `Original Query` and `Baseline Answer`, analyze the `Verification Questions & Answers` to finally filter the refined answer. |
|
|
Original Query: {original_question} |
|
|
Baseline Answer: {baseline_response} |
|
|
Verification Questions & Answer Pairs: |
|
|
{verification_answers} |
|
|
Final Refined Answer:""" |
|
|
|
|
|
|
|
|
try: |
|
|
baseline_response_prompt_template_long = PromptTemplate.from_template(BASELINE_PROMPT_LONG) |
|
|
baseline_response_chain_11 = baseline_response_prompt_template_long | llm_1 | StrOutputParser() |
|
|
baseline_response_chain_12 = baseline_response_prompt_template_long | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate initial answer") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
verification_question_generation_prompt_template_long = PromptTemplate.from_template(VERIFICATION_QUESTION_PROMPT_LONG) |
|
|
verification_question_generation_chain_12 = verification_question_generation_prompt_template_long | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate the verification questionts") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
verification_question_generation_prompt_template_long_simple = PromptTemplate.from_template(VERIFICATION_QUESTION_PROMPT_LONG_simple) |
|
|
verification_question_generation_chain_12_simple = verification_question_generation_prompt_template_long_simple | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate the verification questionts") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
verification_question_generation_prompt_template_long_medium = PromptTemplate.from_template(VERIFICATION_QUESTION_PROMPT_LONG_medium) |
|
|
verification_question_generation_chain_12_medium = verification_question_generation_prompt_template_long_medium | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate the verification questionts") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
verification_question_generation_prompt_template_long_complex = PromptTemplate.from_template(VERIFICATION_QUESTION_PROMPT_LONG_complex) |
|
|
verification_question_generation_chain_12_complex = verification_question_generation_prompt_template_long_complex | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate the verification questionts") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
execution_prompt_self_llm_long = PromptTemplate.from_template(EXECUTE_PLAN_PROMPT_SELF_LLM) |
|
|
execution_prompt_llm_chain_11 = execution_prompt_self_llm_long | llm_1 | StrOutputParser() |
|
|
|
|
|
execution_prompt_llm_chain_13 = execution_prompt_self_llm_long | llm_3 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to execute the verification") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
try: |
|
|
verification_chain_11 = RunnablePassthrough.assign( |
|
|
split_questions=lambda x: x['verification_questions'].split("\n"), |
|
|
) | RunnablePassthrough.assign( |
|
|
answers = (lambda x: [{"verification_question": q} for q in x['split_questions']])| execution_prompt_llm_chain_11.map() |
|
|
) | (lambda x: "\n".join(["Question: {} Answer: {}\n".format(question, answer) for question, answer in zip(x['split_questions'], x['answers'])])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
final_answer_prompt_template_long = PromptTemplate.from_template(FINAL_REFINED_PROMPT) |
|
|
|
|
|
final_answer_chain_12 = final_answer_prompt_template_long | llm_2 | StrOutputParser() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
log.error("Error occured in Chain to generate the final answer") |
|
|
log.error(f"Exception: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chain_long_2 = RunnablePassthrough.assign( |
|
|
baseline_response=baseline_response_chain_12 |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_questions=verification_question_generation_chain_12 |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_answers=verification_chain_11 |
|
|
) | RunnablePassthrough.assign( |
|
|
final_answer=final_answer_chain_12 |
|
|
) |
|
|
|
|
|
chain_long_2_simple = RunnablePassthrough.assign( |
|
|
baseline_response=baseline_response_chain_12 |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_questions=verification_question_generation_chain_12_simple |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_answers=verification_chain_11 |
|
|
) | RunnablePassthrough.assign( |
|
|
final_answer=final_answer_chain_12 |
|
|
) |
|
|
|
|
|
|
|
|
chain_long_2_medium = RunnablePassthrough.assign( |
|
|
baseline_response=baseline_response_chain_12 |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_questions=verification_question_generation_chain_12_medium |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_answers=verification_chain_11 |
|
|
) | RunnablePassthrough.assign( |
|
|
final_answer=final_answer_chain_12 |
|
|
) |
|
|
|
|
|
chain_long_2_complex = RunnablePassthrough.assign( |
|
|
baseline_response=baseline_response_chain_12 |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_questions=verification_question_generation_chain_12_complex |
|
|
) | RunnablePassthrough.assign( |
|
|
verification_answers=verification_chain_11 |
|
|
) | RunnablePassthrough.assign( |
|
|
final_answer=final_answer_chain_12 |
|
|
) |
|
|
|
|
|
|
|
|
retries = 0 |
|
|
max_retries = 10 |
|
|
while retries < max_retries: |
|
|
try: |
|
|
st=time.time() |
|
|
if complexity=="simple": |
|
|
response = chain_long_2_simple.invoke({f"original_question":{text}}) |
|
|
elif complexity=="medium": |
|
|
response = chain_long_2_medium.invoke({f"original_question":{text}}) |
|
|
elif complexity=="complex": |
|
|
response = chain_long_2_complex.invoke({f"original_question":{text}}) |
|
|
response["timetaken"]=round(time.time()-st,3) |
|
|
|
|
|
return response |
|
|
except openai.RateLimitError as RL: |
|
|
|
|
|
retries += 1 |
|
|
if(retries > max_retries): |
|
|
return "Rate Limit Error" |
|
|
wait_time = 2 ** retries |
|
|
print(f"Rate limit exceeded. Retrying in {wait_time} seconds...") |
|
|
time.sleep(wait_time) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except openai.BadRequestError as BRE: |
|
|
log.error(f"Exception: {BRE}") |
|
|
print("Invalid Request Error") |
|
|
return str(BRE) |
|
|
except Exception as e: |
|
|
log.error("Error occured in cov") |
|
|
log.error(f"Exception: {e}") |
|
|
|