Spaces:
Runtime error
Runtime error
File size: 6,963 Bytes
6f31603 797e682 7c0bf42 aa065e1 7c0bf42 797e682 aa065e1 797e682 7c0bf42 85fd688 7c0bf42 797e682 7c0bf42 797e682 aa065e1 797e682 85fd688 797e682 7c0bf42 aa065e1 7c0bf42 797e682 aa065e1 7c0bf42 aa065e1 7c0bf42 aa065e1 797e682 85fd688 7c0bf42 797e682 aa065e1 797e682 7c0bf42 797e682 aa065e1 797e682 7253de2 7c0bf42 797e682 aa065e1 797e682 7c0bf42 aa065e1 7c0bf42 797e682 aa065e1 797e682 7253de2 7c0bf42 7253de2 7c0bf42 7253de2 7c0bf42 797e682 7c0bf42 aa065e1 797e682 7c0bf42 797e682 aa065e1 797e682 85fd688 797e682 7c0bf42 797e682 aa065e1 797e682 aa065e1 797e682 85fd688 797e682 aa065e1 797e682 7c0bf42 797e682 aa065e1 797e682 aa065e1 797e682 85fd688 7c0bf42 85fd688 797e682 6f31603 797e682 6f31603 797e682 aa065e1 6f31603 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1",
"metadata": {},
"outputs": [],
"source": [
"import argparse\n",
"import os\n",
"from pathlib import Path\n",
"from typing import Annotated, List\n",
"\n",
"from dotenv import load_dotenv\n",
"from langchain.tools import tool\n",
"from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage\n",
"from langchain_openai import ChatOpenAI\n",
"from langgraph.graph import StateGraph, START\n",
"from langgraph.graph.message import add_messages\n",
"from langgraph.prebuilt import ToolNode, tools_condition\n",
"from typing_extensions import TypedDict"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"import os\n",
"os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "markdown",
"id": "3",
"metadata": {},
"source": [
"# Tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"def transcribe_audio(audio_path: str) -> str:\n",
" \"\"\"Transcribe the supplied audio file to text using the OpenAI Whisper API (``whisper-1``).\n",
"\n",
" Args:\n",
" audio_path: The path to the audio file to transcribe.\n",
"\n",
" Returns:\n",
" The transcribed text.\n",
"\n",
" Raises:\n",
" RuntimeError: If the ``OPENAI_API_KEY`` environment variable is not set or the API call fails.\n",
" \"\"\"\n",
" if not Path(audio_path).exists():\n",
" return f\"Error: Audio file not found at {audio_path}\"\n",
"\n",
" api_key = os.getenv(\"OPENAI_API_KEY\")\n",
" if not api_key:\n",
" raise RuntimeError(\"OPENAI_API_KEY environment variable is not set.\")\n",
"\n",
" try:\n",
" from openai import OpenAI # type: ignore\n",
"\n",
" client = OpenAI(api_key=api_key)\n",
" with open(audio_path, \"rb\") as f:\n",
" transcription = client.audio.transcriptions.create(\n",
" model=\"whisper-1\",\n",
" file=f,\n",
" )\n",
" text: str | None = getattr(transcription, \"text\", None)\n",
" if text:\n",
" return text.strip()\n",
" raise RuntimeError(\"Transcription response did not contain text.\")\n",
" except Exception as exc:\n",
" raise RuntimeError(f\"OpenAI transcription failed: {exc}\") from exc\n"
]
},
{
"cell_type": "markdown",
"id": "5",
"metadata": {},
"source": [
"# Agent"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"class State(TypedDict):\n",
" messages: Annotated[List[BaseMessage], add_messages]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"default_system_prompt = Path(\"system_promt.txt\").read_text(encoding=\"utf-8\") \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"nebius_api_key = os.environ.get(\"NEBIUS_API_KEY\")\n",
"llm = ChatOpenAI(\n",
" model=\"Qwen/Qwen3-14B\",\n",
" api_key=nebius_api_key,\n",
" base_url=\"https://api.studio.nebius.com/v1/\",\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"tools = [transcribe_audio]\n",
"llm_with_tools = llm.bind_tools(tools)"
]
},
{
"cell_type": "markdown",
"id": "10",
"metadata": {},
"source": [
"## Nodes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"def assistant_node(state: State):\n",
" \"\"\"The assistant node in the graph. It calls the LLM with the current state\n",
" to decide the next action (respond or call a tool).\n",
" \"\"\"\n",
" messages = state[\"messages\"]\n",
" system_message = SystemMessage(content=default_system_prompt)\n",
"\n",
" if not messages or not isinstance(messages[0], SystemMessage):\n",
" messages.insert(0, system_message)\n",
"\n",
" response = llm_with_tools.invoke(messages)\n",
" return {\"messages\": [response]}\n"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"## Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"graph_builder = StateGraph(State)\n",
"graph_builder.add_node(\"assistant\", assistant_node)\n",
"graph_builder.add_node(\"tools\", ToolNode(tools))\n",
"\n",
"graph_builder.add_edge(START, \"assistant\")\n",
"graph_builder.add_conditional_edges(\"assistant\", tools_condition)\n",
"graph_builder.add_edge(\"tools\", \"assistant\")\n",
"\n",
"graph = graph_builder.compile()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"metadata": {},
"outputs": [],
"source": [
"# visualise\n",
"from IPython.display import Image, display\n",
"\n",
"try:\n",
" display(Image(graph.get_graph().draw_mermaid_png()))\n",
"except Exception:\n",
" # This requires some extra dependencies and is optional\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {},
"outputs": [],
"source": [
"question_text = \"What are the filling ingredients in the audio file?\"\n",
"audio_path = \"questions/files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3\"\n",
"\n",
"answer = graph.invoke({\"messages\": question_text, \"audio_path\": audio_path})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"metadata": {},
"outputs": [],
"source": [
"# Show the messages\n",
"for m in answer['messages']:\n",
" m.pretty_print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|