from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableLambda INTENT_TEMPLATE_STR = """ You are an assistant whose job is to identify the user's intent. Given the message below, return a short phrase describing what they want. User message: {user_message} """ FORMAT_TEMPLATE_STR = """ You are a friendly book guide. Using the raw book results below, create a clear and beginner friendly reply. Rules: Return up to three books, Each line should contain the title, the author, and a short summary, Keep the language simple. Raw results: {raw_results} """ intent_prompt = PromptTemplate.from_template(INTENT_TEMPLATE_STR) format_prompt = PromptTemplate.from_template(FORMAT_TEMPLATE_STR) intent_chain = RunnableLambda( lambda user_message: intent_prompt.format(user_message=user_message) ) | RunnableLambda( lambda prompt_text: call_ollama_direct(prompt_text) ) format_chain = RunnableLambda( lambda raw_text: format_prompt.format(raw_results=raw_text) ) | RunnableLambda( lambda prompt_text: call_ollama_direct(prompt_text) ) def extract_intent(user_message): result = intent_chain.invoke(user_message) return str(result).strip() def format_recommendations(raw_results): cleaned =pretty_raw_results(raw_results) result = format_chain.invoke(cleaned) return str(result).strip() # to test intent # from ai_agent import extract_intent print(extract_intent("Find books similar to Atomic Habits")) # to test recommendation # from ai_agent import format_recommendations, fetch_openlibrary_by_title_or_subject res = fetch_openlibrary_by_title_or_subject("mindfulness") print(format_recommendations(res))