{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from modules.llm_in_use import get_llm\n", "from langchain_ollama import ChatOllama\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "llm = get_llm()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain_core.messages import AIMessage\n", "\n", "messages = [\n", " (\n", " \"system\",\n", " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", " ),\n", " (\"human\", \"I love programming.\"),\n", "]\n", "ai_msg = llm.invoke(messages)\n", "ai_msg" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "prompt = ChatPromptTemplate.from_messages(\n", " [\n", " (\n", " \"system\",\n", " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", " ),\n", " (\"human\", \"{input}\"),\n", " ]\n", ")\n", "\n", "chain = prompt | llm\n", "chain.invoke(\n", " {\n", " \"input_language\": \"English\",\n", " \"output_language\": \"German\",\n", " \"input\": \"I love programming.\",\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from typing import List\n", "\n", "from langchain_core.tools import tool\n", "from langchain_ollama import ChatOllama\n", "\n", "\n", "@tool\n", "def validate_user(user_id: int, addresses: List[str]) -> bool:\n", " \"\"\"Validate user using historical addresses.\n", "\n", " Args:\n", " user_id (int): the user ID.\n", " addresses (List[str]): Previous addresses as a list of strings.\n", " \"\"\"\n", " return True\n", "\n", "\n", "llm = ChatOllama(\n", " model=\"llama3.2\",\n", " temperature=0,\n", " base_url=\"http://141.211.127.171\"\n", ").bind_tools([validate_user])\n", "\n", "result = llm.invoke(\n", " \"Could you validate user 123? They previously lived at \"\n", " \"123 Fake St in Boston MA and 234 Pretend Boulevard in \"\n", " \"Houston TX.\"\n", ")\n", "result.tool_calls" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "paintrek", "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.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }