Files
LangChainPractice/first_chain.ipynb

129 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"id": "d581e8a1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DONE\n",
"Imagine a dish that captures the very essence of rustic French comfort: **Boeuf Bourguignon**. This iconic stew features tender chunks of beef slow-\n",
"simmered until they are melt-in-your-mouth soft, bathed in a rich, velvet sauce made from full-bodied red wine and savory stock. Sautéed with earthy\n",
"mushrooms, buttery pearl onions, and smoky lardons, every spoonful offers a complex symphony of flavors that deepens and intensifies with each hour on\n",
"the stove. It is more than just a meal; it is a masterpiece of patience and flavor that transforms simple ingredients into a soulful, sophisticated\n",
"feast.\n"
]
}
],
"source": [
"from langchain_ollama import ChatOllama\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.caches import InMemoryCache \n",
"from langchain_core.globals import set_llm_cache\n",
"import textwrap\n",
"\n",
"set_llm_cache(InMemoryCache())\n",
"\n",
"#Initialize the language model\n",
"llm = ChatOllama(\n",
" model=\"gemma4:12b-mlx\",\n",
" base_url=\"http://mini2.dorceus.local:11434\"\n",
")\n",
"llm2 = ChatOllama(\n",
" model=\"qwen3.5:35b-mlx\",\n",
" base_url=\"http://studio1.dorceus.local:11434\"\n",
")\n",
"\n",
"llm_with_retry = llm.with_retry(\n",
" stop_after_attempt= 3\n",
")\n",
"llm2_with_retry = llm2.with_retry(stop_after_attempt=3)\n",
"\n",
"runner = llm_with_retry.with_fallbacks([llm2_with_retry])\n",
"\n",
"#Create a prompt template\n",
"prompt = ChatPromptTemplate.from_template(\n",
" \"Write a brief, engaging paragraph about {topic}\"\n",
")\n",
"\n",
"prompt2 = ChatPromptTemplate.from_template(\n",
" \"Summarize the following in one sentence : {paragraph}\"\n",
")\n",
"\n",
"prompt3 = ChatPromptTemplate.from_template(\n",
" \"Suggest some side dished that goes with the following food description : {food_description}\"\n",
")\n",
"\n",
"#Create the chain \n",
"# chain = prompt | llm\n",
"# chain2 = prompt2 | llm2\n",
"# chain3 = prompt3 | llm2 \n",
"\n",
"#sequential chain\n",
"# seq_chain = (\n",
"# {\n",
"# \"paragraph\" : prompt,\n",
"# \"food_description\" : prompt2\n",
"# }\n",
"# | prompt2 \n",
"# | prompt3 \n",
"# | llm2\n",
"# )\n",
"\n",
"seq_chain = (\n",
" prompt\n",
" | (lambda message : {\"paragraph\" : message.content})\n",
" | prompt2 \n",
" | (lambda message : {\"food_description\" : message.content})\n",
" | prompt3 \n",
" | runner\n",
")\n",
"\n",
"response = None\n",
"for i in range(1,5) :\n",
" response = chain.invoke(\n",
" {\"topic\": \"Boeuf bourguignon\"}\n",
" )\n",
" #print(response.content)\n",
"\n",
"print(\"DONE\")\n",
"print(textwrap.fill(response.content, 150) )\n",
"\n",
"#Run the chain\n",
"# result = chain.invoke({\"topic\": \"adobong baboy\"})\n",
"# result2 = chain2.invoke({\"topic\": result.content})\n",
"# result3 = chain3.invoke({\"food_description\" : result2.content})\n",
"# print(result.content)\n",
"# print(result2.content)\n",
"# print(result3.content)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}