LangChainPractice/first_chain.ipynb

126 lines
5.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "d581e8a1",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'RunnableRetry' object has no attribute 'with_fallback'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 23\u001b[39m\n\u001b[32m 19\u001b[39m stop_after_attempt= \u001b[32m3\u001b[39m\n\u001b[32m 20\u001b[39m )\n\u001b[32m 21\u001b[39m llm2_with_retry = llm2.with_retry(stop_after_attempt=\u001b[32m3\u001b[39m)\n\u001b[32m 22\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m23\u001b[39m runner = llm_with_retry.with_fallback(llm2_with_retry)\n\u001b[32m 24\u001b[39m \n\u001b[32m 25\u001b[39m \u001b[38;5;66;03m#Create a prompt template\u001b[39;00m\n\u001b[32m 26\u001b[39m prompt = ChatPromptTemplate.from_template(\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/Projects/longchain/first_chain/lib/python3.14/site-packages/pydantic/main.py:1042\u001b[39m, in \u001b[36mBaseModel.__getattr__\u001b[39m\u001b[34m(self, item)\u001b[39m\n\u001b[32m 1039\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m().\u001b[34m__getattribute__\u001b[39m(item) \u001b[38;5;66;03m# Raises AttributeError if appropriate\u001b[39;00m\n\u001b[32m 1040\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 1041\u001b[39m \u001b[38;5;66;03m# this is the current error\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m1042\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\u001b[33mf\u001b[39m\u001b[33m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28mself\u001b[39m).\u001b[34m__name__\u001b[39m\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[33m object has no attribute \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mitem\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[33m'\u001b[39m)\n",
"\u001b[31mAttributeError\u001b[39m: 'RunnableRetry' object has no attribute 'with_fallback'"
]
}
],
"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",
"\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",
"for i in range(1,5) :\n",
" response = chain.invoke(\n",
" {\"topic\": \"adobong baboy\"}\n",
" )\n",
" #print(response.content)\n",
"\n",
"print(\"DONE\")\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": "first_chain",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}