LangChain is a framework for connecting language models with prompts and application context. A Python function can use its OllamaLLM interface to send a structured prompt and retrieved Postgres data to TinyLlama. section 8.6.2
Listing 8.15 Answering user questions using the LLM and provided context
def answer_question(question, context):
# Connecting to the tinyllama model with the LangChain Ollama interface
llm = OllamaLLM(model="tinyllama", temperature=0.6)
# Generating a final prompt for the LLM based on considering
# the provided context
prompt = f"""
You're a movie expert and your task is to answer questions about movies
based on the provided context.
This is the user's question: {question}
Consider the following context to provide a detailed and
accurate answer: {context}
The context includes the following details for each movie:
- "Title" of the movie
- "Vote Average" - the average rating of the movie
- "Budget" - the budget allocated for the movie in US dollars
- "Revenue" - the total revenue generated by the movie in US dollars
- "Release Date" - the date the movie was released
Respond in an engaging style that inspires the user to watch the movies.
"""
# Invoke the LLM passing the prompt. The LLM will generate a response.
response = llm.invoke(prompt)
return response
Use the runnable pattern to define answer_question(question, context), configure TinyLlama, insert question and context into a prompt, and call llm.invoke(prompt). section 8.6.2
Retrieval-augmented movie answers
A RAG workflow retrieves movie records from Postgres for the question “I'd like to watch the best movies about pirates,” then passes that context to LangChain and prints the generated answer. Retrieved results include Pirates of the Caribbean films with ratings, budgets, revenue, and release dates. section 8.6.4


















