Upload app.py with huggingface_hub
Browse files79 unmodified lines80818283848586878889909192939463 unmodified lines15815916016116216316416516616716816917017117262 unmodified lines23523623723823924024124224324424524624754 unmodified lines30230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133231 unmodified lines3643653663673683693703713723732 unmodified lines37637737837938038138220 unmodified lines40340440540640740840941079 unmodified linesmodel='text-embedding-ada-002')# Initialize the Chat OpenAI model# llm = ChatOpenAI(# base_url=openai_api_base,# openai_api_key=openai_api_key,# model="gpt-4o-mini",# streaming=False# )llm = ChatOpenAI(base_url=openai_api_base,openai_api_key=openai_api_key,63 unmodified linesreturn state# Initialize the Chroma vector store for retrieving documents# vector_store = Chroma(# collection_name="nutritional_hypotheticals",# persist_directory="./nutritional_db",# embedding_function=embedding_model# )# HuggingFace expects to create this so remove the old oneDB_DIR = "./nutritional_db"62 unmodified lines("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")])# chain = response_prompt | llm# response = chain.invoke({# "query": state['query'],# "context": "\n".join([doc["content"] for doc in state['context']]),# "feedback": state['feedback'] # add feedback to the prompt# })# Build the context string separately for claritycontext_str = "\n".join(doc["content"] for doc in state["context"])54 unmodified lines])# chain = groundedness_prompt | llm | StrOutputParser()# groundedness_score = float(chain.invoke({# "context": "\n".join([doc["content"] for doc in state['context']]),# "response": state['response'] # Complete the code to define the response# }))# Build the input payload for the groundedness chain# groundedness_input = {# "context": context_str,# "response": state["response"], # Complete the code to define the response# }# Run the chain and parse the groundedness scorechain = groundedness_prompt | llm | StrOutputParser()# groundedness_score = float(chain.invoke(groundedness_input))groundedness_score = float(chain.invoke({"context": context_str,"response": response_str}))# if not context:# print(f"[INFO] No documents found for query: {query}")if not state.get('context'):print("[INFO] No documents in state['context']")31 unmodified lines("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")])chain = precision_prompt | llm | StrOutputParser() # Complete the code to define the chain of processingprecision_score = float(chain.invoke({"query": state['query'],"response":state['response'] # Complete the code to access the response from the state}))state['precision_score'] = precision_scorestate['precision_loop_count'] +=12 unmodified linesdef refine_response(state: Dict) -> Dict: #Blank #6 for app.py"""Suggests improvements for the generated response.20 unmodified lineschain = refine_response_prompt | llm| StrOutputParser()# Store response suggestions in a structured format# feedback = f"Previous Response: {state['response']}\nSuggestions: {chain.invoke({'query': state['query'], 'context': "\n".join([doc["content"] for doc in state['context']]), 'response': state['response']})}"context_str = "\n".join(doc["content"] for doc in state["context"])suggestions = chain.invoke({"query": state["query"],79 unmodified lines80818283848563 unmodified lines14915015115215315462 unmodified lines21721821922022122254 unmodified lines27727827928028128228328428528628728828929029131 unmodified lines3233243253263273283293303313322 unmodified lines33533633733833934034120 unmodified lines36236336436536636779 unmodified linesmodel='text-embedding-ada-002')llm = ChatOpenAI(base_url=openai_api_base,openai_api_key=openai_api_key,63 unmodified linesreturn state# HuggingFace expects to create this so remove the old oneDB_DIR = "./nutritional_db"62 unmodified lines("user", "Query: {query}\nContext: {context}\n\nfeedback: {feedback}")])# Build the context string separately for claritycontext_str = "\n".join(doc["content"] for doc in state["context"])54 unmodified lines])# Run the chain and parse the groundedness scorechain = groundedness_prompt | llm | StrOutputParser()groundedness_score = float(chain.invoke({"context": context_str,"response": response_str}))if not state.get('context'):print("[INFO] No documents in state['context']")31 unmodified lines("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")])chain = precision_prompt | llm | StrOutputParser()precision_score = float(chain.invoke({"query": state['query'],"response":state['response']}))state['precision_score'] = precision_scorestate['precision_loop_count'] +=12 unmodified linesdef refine_response(state: Dict) -> Dict:"""Suggests improvements for the generated response.20 unmodified lineschain = refine_response_prompt | llm| StrOutputParser()# Store response suggestions in a structured formatcontext_str = "\n".join(doc["content"] for doc in state["context"])suggestions = chain.invoke({"query": state["query"],
app.py+4-47