Upload app.py with huggingface_hub
Browse files79 unmodified lines80818283848586878889909183848563 unmodified lines14915015116116216316416516616716816915215315462 unmodified lines21721821923823924024124224324422022122254 unmodified lines27727827930530630730830931031131231331431531631728028132028228328428528632632728732928828929029131 unmodified lines3233243253673263273283703293303313322 unmodified lines33533633737933833934034120 unmodified lines36236336440640736536636779 unmodified lines model='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 lines return 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 clarity context_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 score chain = 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']") 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 processing chain = precision_prompt | llm | StrOutputParser() precision_score = float(chain.invoke({ "query": state['query'], "response":state['response'] # Complete the code to access the response from the state "response":state['response'] })) state['precision_score'] = precision_score state['precision_loop_count'] +=12 unmodified lines
def refine_response(state: Dict) -> Dict: #Blank #6 for app.pydef refine_response(state: Dict) -> Dict: """ Suggests improvements for the generated response.
20 unmodified lines chain = 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"],app.py+4-47