{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "787728aa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello! ๐Ÿ‘‹ How can I help you today?\n" ] } ], "source": [ "from ollama import chat\n", "\n", "response = chat(\n", " model='qwen3.5:4b',\n", " messages=[{'role': 'user', 'content': 'Hello!'}],\n", ")\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": 3, "id": "851a76dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ํ˜„์žฌ ๋Œ€ํ•œ๋ฏผ๊ตญ ๋Œ€ํ†ต๋ น์€ **์œค์„์—ด** (Yoon Suk-yeol)์ž…๋‹ˆ๋‹ค.\n", "\n", "2022 ๋…„ 5 ์›” 10 ์ผ 10 ๋Œ€ ๋Œ€ํ†ต๋ น์œผ๋กœ ์ทจ์ž„ํ•˜์—ฌ ์žฌ์ง ์ค‘์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋Œ€์„ ์€ 2027 ๋…„์ด ์˜ˆ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.\n" ] } ], "source": [ "from ollama import chat\n", "\n", "response = chat(\n", " model='qwen3.5:4b',\n", " messages=[{'role': 'user', 'content': '๋Œ€ํ•œ๋ฏผ๊ตญ ๋Œ€ํ†ต๋ น์€ ๋ˆ„๊ตฌ์•ผ?'}],\n", ")\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "055cf9a8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello! How can I assist you today? ๐Ÿ˜Š\n" ] } ], "source": [ "from ollama import chat\n", "\n", "response = chat(\n", " model='deepseek-r1:1.5b',\n", " messages=[{'role': 'user', 'content': 'Hello!'}],\n", ")\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "accd1b97", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To solve this problem, we need to generate the Fibonacci sequence up to the nth term efficiently. The Fibonacci sequence is defined such that each number is the sum of the two preceding ones, starting from 0 and 1. \n", "\n", "### Approach\n", "The approach used here is an iterative method to compute the Fibonacci numbers. This method is chosen for its efficiency in terms of both time and space complexity compared to a recursive approach. Here are the key steps:\n", "\n", "1. **Initialization**: Start with the first two Fibonacci numbers, 0 and 1.\n", "2. **Iteration**: For each subsequent term up to n (excluding the initial two), compute the next number as the sum of the previous two terms.\n", "3. **Update**: After computing a new term, update the variables holding the last two terms for the next iteration.\n", "\n", "This approach ensures that we do not store all previously computed values in memory, which optimizes both time and space complexity.\n", "\n", "### Solution Code\n", "```python\n", "def fibonacci(n):\n", " if n <= 0:\n", " raise ValueError(\"n must be a positive integer\")\n", " elif n == 1:\n", " return 0\n", " elif n == 2:\n", " return 1\n", " else:\n", " a, b = 0, 1\n", " for _ in range(3, n + 1):\n", " c = a + b\n", " a = b\n", " b = c\n", " return b\n", "```\n", "\n", "### Explanation\n", "- **Initialization**: We start with the first two Fibonacci numbers: `a = 0` and `b = 1`.\n", "- **Iteration Loop**: For each term from 3 to n, we compute the next Fibonacci number as `c = a + b`. Then, update `a` to be the previous `b`, and `b` to be the newly computed `c`.\n", "- **Return Result**: After completing the loop up to n terms, return `b` which is the nth Fibonacci number.\n", "\n", "This method efficiently computes each term in linear time and constant space, making it suitable for large values of n.\n" ] } ], "source": [ "from ollama import chat\n", "\n", "response = chat(\n", " model='deepseek-r1:1.5b',\n", " messages=[{'role': 'user', 'content': 'ํŒŒ์ด์ฌ์œผ๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ˜„ํ•ด์ค˜!'}],\n", ")\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "e332d754", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "๋ฌผ๋ก ์ž…๋‹ˆ๋‹ค! ํŒŒ์ด์ฌ์œผ๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.\n", "\n", "### 1. ์žฌ๊ท€ ํ•จ์ˆ˜ ์‚ฌ์šฉ\n", "\n", "์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค:\n", "\n", "```python\n", "def fibonacci_recursive(n):\n", " if n <= 0:\n", " return \"ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\"\n", " elif n == 1:\n", " return 0\n", " elif n == 2:\n", " return 1\n", " else:\n", " return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)\n", "\n", "# ์˜ˆ์‹œ ํ˜ธ์ถœ\n", "for i in range(1, 11):\n", " print(f\"fibonacci({i}) = {fibonacci_recursive(i)}\")\n", "```\n", "\n", "### 2. ๋ฐ˜๋ณต ๊ตฌ์กฐ ์‚ฌ์šฉ\n", "\n", "๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•œ ํšจ์œจ์ ์ธ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค:\n", "\n", "```python\n", "def fibonacci_iterative(n):\n", " if n <= 0:\n", " return \"ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\"\n", " \n", " a, b = 0, 1\n", " for _ in range(2, n + 1):\n", " a, b = b, a + b\n", " \n", " return b\n", "\n", "# ์˜ˆ์‹œ ํ˜ธ์ถœ\n", "for i in range(1, 11):\n", " print(f\"fibonacci({i}) = {fibonacci_iterative(i)}\")\n", "```\n", "\n", "### 3. ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ์‚ฌ์šฉ (Top-Down ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ)\n", "\n", "์žฌ๊ท€ ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค:\n", "\n", "```python\n", "def fibonacci_memoization(n, memo={}):\n", " if n <= 0:\n", " return \"ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\"\n", " \n", " if n in memo:\n", " return memo[n]\n", " \n", " if n == 1:\n", " result = 0\n", " elif n == 2:\n", " result = 1\n", " else:\n", " result = fibonacci_memoization(n-1, memo) + fibonacci_memoization(n-2, memo)\n", " \n", " memo[n] = result\n", " return result\n", "\n", "# ์˜ˆ์‹œ ํ˜ธ์ถœ\n", "for i in range(1, 11):\n", " print(f\"fibonacci({i}) = {fibonacci_memoization(i)}\")\n", "```\n", "\n", "### 4. ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ (Bottom-Up)\n", "\n", "๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ์„ ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:\n", "\n", "```python\n", "def fibonacci_dp(n):\n", " if n <= 0:\n", " return \"ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\"\n", " \n", " fib = [0, 1] + [0] * (n - 1)\n", " for i in range(2, n + 1):\n", " fib[i] = fib[i-1] + fib[i-2]\n", " \n", " return fib[n]\n", "\n", "# ์˜ˆ์‹œ ํ˜ธ์ถœ\n", "for i in range(1, 11):\n", " print(f\"fibonacci({i}) = {fibonacci_dp(i)}\")\n", "```\n", "\n", "์œ„์˜ ๊ฐ ์˜ˆ์ œ๋Š” ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. `n` ๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๋ฐฉ์‹์„ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.\n" ] } ], "source": [ "from ollama import chat\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role': 'user', 'content': 'ํŒŒ์ด์ฌ์œผ๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ˜„ํ•ด์ค˜!'}],\n", ")\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "e1790fc7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "๋ถ€์ •.\n" ] } ], "source": [ "messages = [\n", " # ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ\n", " {\"role\" : \"system\", \"content\" : \"๋„ˆ๋Š” ๋ฌธ์žฅ์˜ ๊ฐ์„ฑ์„ ๊ธ์ •, ๋ถ€์ •, ์ค‘๋ฆฝ ์ค‘ ํ•˜๋‚˜๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ai ์•ผ\"},\n", " # ํ“จ์ƒท\n", " {\"role\" : \"user\", \"content\" : \"์ •๋ง ๋งŒ์กฑ์Šค๋Ÿฌ์šด ์„œ๋น„์Šค์˜€์–ด์š”.\"}, \n", " {\"role\" : \"assistant\", \"content\" : \"๊ธ์ •\"}, \n", " {\"role\" : \"user\", \"content\" : \"๋‹ค์‹œ๋Š” ์ด์šฉ ์•ˆํ•  ๊ฒƒ ๊ฐ™์•„์š”.\"}, \n", " {\"role\" : \"assistant\", \"content\" : \"๋ถ€์ •.\"}, \n", " # ์‚ฌ์šฉ์ž ํ”„๋กฌํ”„ํŠธ\n", " {\"role\" : \"user\", \"content\" : \"๋ฐฐ์†ก์ด ๋„ˆ๋ฌด ๋Šฆ์—ˆ์–ด์š”.\"}, \n", "]\n", "\n", "response = chat(model='qwen2.5', messages=messages)\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "837d9824", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "์•ˆ๋…•ํ•˜์„ธ์š”! ์ €๋Š” Qwen์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ตญ์–ด์™€ ํ•œ๊ตญ์–ด๋ฅผ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ์–ธ์–ด๋กœ ๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์ •๋ณด ์ œ๊ณต๊ณผ ์งˆ๋ฌธ ๋‹ต๋ณ€์„ ํ†ตํ•ด ๋„์›€์ด ๋˜๋Š” ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜ ๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?" ] } ], "source": [ "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role': 'user', 'content': '์•ˆ๋…• ๊ฐ„๋‹จํžˆ ์ž๊ธฐ์†Œ๊ฐœ ํ•ด๋ด!'}],\n", " stream=True\n", ")\n", "\n", "for chunk in response:\n", " content = chunk['message']['content']\n", " print(content, end='', flush=True)" ] }, { "cell_type": "markdown", "id": "f89a855d", "metadata": {}, "source": [ "### ๊ตฌ์กฐํ™”๋œ ์ถœ๋ ฅ(Structured Output)\n", "- json ํ˜•ํƒœ\n", " - ์‚ฌ์šฉ์ž ํ”„๋กฌํ”„ํŠธ์— ์ง€์ •\n", " - json ํ˜•์‹\n", " - pydantic" ] }, { "cell_type": "code", "execution_count": null, "id": "07c11d11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"sentiment\": \"positive\",\n", " \"score\": 1\n", "}\n" ] } ], "source": [ "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋ฌธ์žฅ์˜ ๊ฐ์ •์„ ๋ถ„์„ํ•˜๋ผ.\n", "JSON ์œผ๋กœ๋งŒ ๋ฐ˜ํ™˜ํ•˜์‹œ์˜ค.\n", "\n", "\n", "{\n", " \"sentiment\" :\"\",\n", " \"score\":0\n", "}\n", "\n", "๋ฌธ์žฅ:\n", "์˜ค๋Š˜ ์ •๋ง ํ–‰๋ณตํ•œ ํ•˜๋ฃจ์˜€๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}]\n", "\n", ")\n", "\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "07c11d11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"summary\": \"๊ฒฝ์ฐฐ์ด 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ ๋‚ด์šฉ์„ ํฌํ•จํ•œ ํ•ฉ์„ฑ๋ฌผ์„ ๋งŒ๋“  50๋Œ€ ์—ฌ์„ฑ A์”จ๋ฅผ ์ฒดํฌํ–ˆ์Šต๋‹ˆ๋‹ค.\",\n", " \"sentiment\": \"์ค‘๋ฆฝ\",\n", " \"keywords\": [\"5ยท18 ๋ฏผไธปๅŒ–้‹ๅ‹•\", \"ๅˆๆˆ็‰ฉ\", \"ๆ้€ ๆ–ฐ้—ป\", \"ๆ‹˜็•™\", \"ๅฅณๆ€ง\"]\n", "}\n" ] } ], "source": [ "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋‰ด์Šค๊ธฐ์‚ฌ๋ฅผ ๋ถ„์„ํ•˜๋ผ.\n", "JSON ์œผ๋กœ๋งŒ ๋ฐ˜ํ™˜ํ•˜์‹œ์˜ค.\n", "\n", "\n", "{\n", " \"summary\" : \"\",\n", " \"sentiment\" :\"\",\n", " \"keywords\":[]\n", "}\n", "\n", "๊ธฐ์‚ฌ:\n", "์‹ ๋ฌธ ๊ธฐ์‚ฌ๋ฅผ ๋ชจ๋ฐฉํ•œ ํ•ฉ์„ฑ๋ฌผ๋กœ 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ ๋ˆ„๋ฆฌ๊พผ์ด ๊ฒฝ์ฐฐ์— ๋ถ™์žกํ˜”๋‹ค.\n", "\n", "๊ด‘์ฃผ๊ฒฝ์ฐฐ์ฒญ ์‚ฌ์ด๋ฒ„๋ฒ”์ฃ„์ˆ˜์‚ฌ๋Œ€๋Š” 24์ผ 5ยท18๋ฏผ์ฃผํ™”์šด๋™ ๋“ฑ์— ๊ด€ํ•œ ํŠน๋ณ„๋ฒ• ์œ„๋ฐ˜, ๋ช…์˜ˆํ›ผ์†, ์—…๋ฌด๋ฐฉํ•ด ๋“ฑ ํ˜์˜๋กœ 50๋Œ€ ์—ฌ์„ฑ A์”จ๋ฅผ ์ž…๊ฑดํ–ˆ๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}]\n", "\n", ")\n", "\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "0eb2c319", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"sentiment\": \"์–‘ํ˜ธ\",\n", " \"score\": 0\n", "}\n" ] } ], "source": [ "schema = {\n", " \"type\":\"object\",\n", " \"properties\":{\n", " \"sentiment\":{\"type\":\"string\",\"description\":\"sentiment\"},\n", " \"score\":{\"type\":\"integer\", \"description\":\"score\"},\n", " }\n", "}\n", "\n", "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋ฌธ์žฅ์˜ ๊ฐ์ •์„ ๋ถ„์„ํ•˜๋ผ.\n", "๋‹จ, format์œผ๋กœ ์ฃผ์–ด์ง„ ๊ตฌ์กฐ๋ฅผ ์ถœ๋ ฅํ•ด์ค˜\n", "\n", "๋ฌธ์žฅ:\n", "์˜ค๋Š˜ ์ •๋ง ํ–‰๋ณตํ•œ ํ•˜๋ฃจ์˜€๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}],\n", " format=schema\n", ")\n", "\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "4be52eed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"summary\": \"๊ฒฝ์ฐฐ์ด ๋ชจ๋ฐฉ ์‹ ๋ฌธ ๊ธฐ์‚ฌ๋กœ 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ 50๋Œ€ ์—ฌ์„ฑ A์”จ๋ฅผ ์ฒดํฌํ•˜์˜€๋‹ค๋Š” ์†Œ์‹์ž…๋‹ˆ๋‹ค.\",\n", " \"sentiment\": \"neutral\",\n", " \"keywords\": [\"5ยท18๋ฏผ์ฃผํ™”์šด๋™\", \"๋ช…์˜ˆํ›ผ์†\", \"์‚ฌ์ด๋ฒ„๋ฒ”์ฃ„์ˆ˜์‚ฌ๋Œ€\", \"ํ•ฉ์„ฑ๋ฌผ\", \"๊ฒฝ์ฐฐ ์ฒดํฌ\"]\n", "}\n" ] } ], "source": [ "schema = {\n", " \"type\":\"object\",\n", " \"properties\":{\n", " \"summary\":{\"type\":\"string\"},\n", " \"sentiment\":{\"type\":\"string\",\"enum\":[\"positive\", \"neutral\", \"negative\"]},\n", " \"keywords\":{\"type\":\"array\"},\n", " },\n", " \"required\":[\n", " \"summary\", \"sentiment\", \"keywords\"\n", " ]\n", "}\n", "\n", "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋‰ด์Šค๊ธฐ์‚ฌ๋ฅผ ๋ถ„์„ํ•˜๋ผ.\n", "๋‹จ, format์œผ๋กœ ์ฃผ์–ด์ง„ ๊ตฌ์กฐ๋กœ ์ถœ๋ ฅํ•ด์ค˜.\n", "ํ•œ๊ธ€๋กœ!\n", "\n", "๊ธฐ์‚ฌ:\n", "์‹ ๋ฌธ ๊ธฐ์‚ฌ๋ฅผ ๋ชจ๋ฐฉํ•œ ํ•ฉ์„ฑ๋ฌผ๋กœ 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ ๋ˆ„๋ฆฌ๊พผ์ด ๊ฒฝ์ฐฐ์— ๋ถ™์žกํ˜”๋‹ค.\n", "\n", "๊ด‘์ฃผ๊ฒฝ์ฐฐ์ฒญ ์‚ฌ์ด๋ฒ„๋ฒ”์ฃ„์ˆ˜์‚ฌ๋Œ€๋Š” 24์ผ 5ยท18๋ฏผ์ฃผํ™”์šด๋™ ๋“ฑ์— ๊ด€ํ•œ ํŠน๋ณ„๋ฒ• ์œ„๋ฐ˜, ๋ช…์˜ˆํ›ผ์†, ์—…๋ฌด๋ฐฉํ•ด ๋“ฑ ํ˜์˜๋กœ 50๋Œ€ ์—ฌ์„ฑ A์”จ๋ฅผ ์ž…๊ฑดํ–ˆ๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}],\n", " format=schema\n", ")\n", "\n", "print(response.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "3088b0d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sentiment='๊ธ์ •' score=0.95\n", "๊ธ์ •\n", "0.95\n" ] } ], "source": [ "from pydantic import BaseModel, Field\n", "\n", "class SentimentResult(BaseModel):\n", " sentiment : str\n", " score : float = Field(ge=0.0, le=1.0)\n", " # keywords : list[str]\n", "\n", "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋ฌธ์žฅ์˜ ๊ฐ์ •์„ ๋ถ„์„ํ•˜๋ผ.\n", "๋‹จ, format์œผ๋กœ ์ฃผ์–ด์ง„ ๊ตฌ์กฐ๋ฅผ ์ถœ๋ ฅํ•ด์ค˜\n", "\n", "๋ฌธ์žฅ:\n", "์˜ค๋Š˜ ์ •๋ง ํ–‰๋ณตํ•œ ํ•˜๋ฃจ์˜€๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}],\n", " format=SentimentResult.model_json_schema()\n", ")\n", "\n", "result = SentimentResult.model_validate_json(response.message.content)\n", "print(result)\n", "\n", "print(result.sentiment)\n", "print(result.score)\n", "# print(result.keywords)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "935146a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['5ยท18 ๋ฏผ์ฃผํ™”์šด๋™', 'ํ•ฉ์„ฑ๋ฌผ', '๋ช…์˜ˆํ›ผ์†', '๊ฒฝ์ฐฐ ์ˆ˜์‚ฌ', '50๋Œ€ ์—ฌ์„ฑ A์”จ', '์‚ฌ์ด๋ฒ„๋ฒ”์ฃ„์ˆ˜์‚ฌ๋Œ€']\n", "negative\n", "๊ฒฝ์ฐฐ์ด 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ชจ๋ฐฉ ํ•ฉ์„ฑ๋ฌผ์„ ์ž‘์„ฑํ•œ 50๋Œ€ ์—ฌ์„ฑ์ด ๊ฒ€๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n" ] } ], "source": [ "# \"sentiment\":{\"type\":\"string\",\"enum\":[\"positive\", \"neutral\", \"negative\"]},\n", "\n", "from pydantic import BaseModel, Field\n", "from typing import Literal\n", "\n", "class NewsResult(BaseModel):\n", " summary : str\n", " sentiment : Literal[\"positive\", \"neutral\", \"negative\"]\n", " keywords : list[str]\n", "\n", "prompt = \"\"\"\\\n", "๋‹ค์Œ ๋‰ด์Šค๊ธฐ์‚ฌ๋ฅผ ๋ถ„์„ํ•˜๋ผ.\n", "๋‹จ, format์œผ๋กœ ์ฃผ์–ด์ง„ ๊ตฌ์กฐ๋กœ ์ถœ๋ ฅํ•ด์ค˜.\n", "ํ•œ๊ธ€๋กœ!\n", "\n", "๊ธฐ์‚ฌ:\n", "์‹ ๋ฌธ ๊ธฐ์‚ฌ๋ฅผ ๋ชจ๋ฐฉํ•œ ํ•ฉ์„ฑ๋ฌผ๋กœ 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™์„ ํ„ํ›ผํ•œ ๋ˆ„๋ฆฌ๊พผ์ด ๊ฒฝ์ฐฐ์— ๋ถ™์žกํ˜”๋‹ค.\n", "\n", "๊ด‘์ฃผ๊ฒฝ์ฐฐ์ฒญ ์‚ฌ์ด๋ฒ„๋ฒ”์ฃ„์ˆ˜์‚ฌ๋Œ€๋Š” 24์ผ 5ยท18๋ฏผ์ฃผํ™”์šด๋™ ๋“ฑ์— ๊ด€ํ•œ ํŠน๋ณ„๋ฒ• ์œ„๋ฐ˜, ๋ช…์˜ˆํ›ผ์†, ์—…๋ฌด๋ฐฉํ•ด ๋“ฑ ํ˜์˜๋กœ 50๋Œ€ ์—ฌ์„ฑ A์”จ๋ฅผ ์ž…๊ฑดํ–ˆ๋‹ค.\n", "\"\"\"\n", "\n", "response = chat(\n", " model='qwen2.5',\n", " messages=[{'role':'user', 'content':prompt}],\n", " format=NewsResult.model_json_schema()\n", ")\n", "\n", "result = NewsResult.model_validate_json(response.message.content)\n", "print(result.keywords)\n", "print(result.sentiment)\n", "print(result.summary)" ] }, { "cell_type": "markdown", "id": "734f5312", "metadata": {}, "source": [ "### tool calling\n", "- ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ" ] }, { "cell_type": "code", "execution_count": null, "id": "258bb684", "metadata": {}, "outputs": [], "source": [ "def calculater(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": null, "id": "5a85a23d", "metadata": {}, "outputs": [], "source": [ "tools = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"calculator\",\n", " \"description\": \"๋‘ ์ˆซ์ž๋ฅผ ๋”ํ•œ๋‹ค.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"a\": {\n", " \"type\": \"integer\",\n", " \"description\": \"์ฒซ ๋ฒˆ์งธ ์ˆซ์ž\"\n", " },\n", " \"b\": {\n", " \"type\": \"integer\",\n", " \"description\": \"๋‘ ๋ฒˆ์งธ ์ˆซ์ž\"\n", " }\n", " },\n", " \"required\": [\"a\",\"b\"]\n", " }\n", " }\n", " }\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "4060bab7", "metadata": {}, "outputs": [], "source": [ "response = chat(\n", " mode='qwen2.5',\n", " messages=[{'role':'user', 'content':\"25์™€ 36์„ ๋”ํ•ด์ค˜\"}],\n", " tools=tools\n", ")\n", "\n", "print(respons.message) # \"25์™€ 36์„ ๋”ํ•ด์ค˜\"" ] }, { "cell_type": "code", "execution_count": null, "id": "0372e988", "metadata": {}, "outputs": [], "source": [ "# llm ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด tool ์‹คํ–‰\n", "tool_call = response.message.tool_call[0]\n", "args = tool_call.funcation.arguments\n", "\n", "result = calculator(args['a'], args['b'])\n", "\n", "# tool ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ llm ๋ณด๋‚ด๊ธฐ\n", "\n", "messages = [\n", " {'role': 'user', 'content':\"25์™€ 36์„ ๋”ํ•ด์ค˜\"},\n", " respons.message,\n", " {'role': 'tool', 'content':str(result)},\n", "\n", "]\n", "\n", "final_response = chat(model='qwen2.5', messages=messages)\n", "print(final_respons.message.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "47792959", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "role='assistant' content='' thinking=None images=None tool_name=None tool_calls=[ToolCall(function=Function(name='get_weather', arguments={'city': '์„œ์šธ'}))]\n", "Tool ์‹คํ–‰: get_weather({'city': '์„œ์šธ'}) โ†’ {'temp': 22, 'condition': '๋ง‘์Œ'}\n", "ํ˜„์žฌ ์„œ์šธ์˜ ๊ธฐ์˜จ์€ 22๋„ Celsius๋กœ, ๋‚ ์”จ๋Š” ๋ง‘์Šต๋‹ˆ๋‹ค.\n" ] } ], "source": [ "import ollama\n", "import json\n", "\n", "# โ‘  ์‹ค์ œ๋กœ ์‹คํ–‰๋  ํ•จ์ˆ˜๋“ค\n", "def get_weather(city: str) -> dict:\n", " # ์‹ค์ œ๋กœ๋Š” ๋‚ ์”จ API ํ˜ธ์ถœ โ€” ์—ฌ๊ธฐ์„  ๋”๋ฏธ ๋ฐ์ดํ„ฐ\n", " data = {\n", " \"์„œ์šธ\": {\"temp\": 22, \"condition\": \"๋ง‘์Œ\"},\n", " \"๋ถ€์‚ฐ\": {\"temp\": 25, \"condition\": \"ํ๋ฆผ\"},\n", " }\n", " return data.get(city, {\"temp\": 0, \"condition\": \"์•Œ ์ˆ˜ ์—†์Œ\"})\n", "\n", "# โ‘ก LLM์—๊ฒŒ \"์ด๋Ÿฐ Tool์ด ์žˆ๋‹ค\"๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์Šคํ‚ค๋งˆ\n", "tools = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"get_weather\",\n", " \"description\": \"ํŠน์ • ๋„์‹œ์˜ ํ˜„์žฌ ๋‚ ์”จ๋ฅผ ์กฐํšŒํ•œ๋‹ค\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"city\": {\n", " \"type\": \"string\",\n", " \"description\": \"๋‚ ์”จ๋ฅผ ์กฐํšŒํ•  ๋„์‹œ ์ด๋ฆ„ (์˜ˆ: ์„œ์šธ, ๋ถ€์‚ฐ)\"\n", " }\n", " },\n", " \"required\": [\"city\"]\n", " }\n", " }\n", " }\n", "]\n", "\n", "messages = [{\"role\": \"user\", \"content\": \"์„œ์šธ ๋‚ ์”จ ์•Œ๋ ค์ค˜\"}]\n", "\n", "# โ‘ข LLM ํ˜ธ์ถœ โ€” Tool ๋ชฉ๋ก ์ „๋‹ฌ\n", "response = ollama.chat(\n", " model=\"qwen2.5\",\n", " messages=messages,\n", " tools=tools,\n", ")\n", "\n", "print(response.message)\n", "\n", "# โ‘ฃ LLM์ด Tool ํ˜ธ์ถœ์„ ์š”์ฒญํ–ˆ์œผ๋ฉด ์‹ค์ œ๋กœ ์‹คํ–‰\n", "if response.message.tool_calls:\n", " for tool_call in response.message.tool_calls:\n", " name = tool_call.function.name\n", " args = tool_call.function.arguments\n", "\n", " # ํ•จ์ˆ˜ ์ด๋ฆ„์œผ๋กœ ์‹ค์ œ ํ•จ์ˆ˜ ๋งคํ•‘\n", " tool_map = {\"get_weather\": get_weather}\n", " result = tool_map[name](**args)\n", " print(f\"Tool ์‹คํ–‰: {name}({args}) โ†’ {result}\")\n", "\n", " # โ‘ค Tool ๊ฒฐ๊ณผ๋ฅผ ๋Œ€ํ™” ์ด๋ ฅ์— ์ถ”๊ฐ€\n", " messages.append(response.message) # LLM์˜ tool_calls ๋ฉ”์‹œ์ง€\n", " messages.append({\n", " \"role\": \"tool\",\n", " \"content\": json.dumps(result, ensure_ascii=False),\n", " })\n", "\n", "# โ‘ฅ ๊ฒฐ๊ณผ๋ฅผ ํฌํ•จํ•ด์„œ LLM์— ๋‹ค์‹œ ์ „๋‹ฌ โ†’ ์ตœ์ข… ์ž์—ฐ์–ด ์‘๋‹ต\n", "final = ollama.chat(model=\"qwen2.5\", messages=messages)\n", "print(final.message.content)\n", "# \"์„œ์šธ์˜ ํ˜„์žฌ ๋‚ ์”จ๋Š” 22ยฐC์ด๋ฉฐ ๋ง‘์Šต๋‹ˆ๋‹ค.\"" ] }, { "cell_type": "code", "execution_count": null, "id": "de085430", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'ollama' has no attribute 'chat'", "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 38\u001b[39m\n\u001b[32m 34\u001b[39m messages = [{\u001b[33m\"role\"\u001b[39m: \u001b[33m\"user\"\u001b[39m, \u001b[33m\"content\"\u001b[39m: \u001b[33m\"์„œ์šธ ๋‚ ์”จ ๋ณด๊ณ  ์šฐ์‚ฐ์ด ํ•„์š”ํ•œ์ง€ ์•Œ๋ ค์ค˜\"\u001b[39m}]\n\u001b[32m 35\u001b[39m \n\u001b[32m 36\u001b[39m \u001b[38;5;66;03m# โ”€โ”€ ์ž๋™ ๋ฃจํ”„: Tool ํ˜ธ์ถœ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต โ”€โ”€\u001b[39;00m\n\u001b[32m 37\u001b[39m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m---> \u001b[39m\u001b[32m38\u001b[39m response = ollama.chat(model=\u001b[33m\"qwen2.5\"\u001b[39m, messages=messages, tools=tools)\n\u001b[32m 39\u001b[39m \n\u001b[32m 40\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28;01mnot\u001b[39;00m response.message.tool_calls:\n\u001b[32m 41\u001b[39m \u001b[38;5;66;03m# Tool ํ˜ธ์ถœ ์—†์Œ = ์ตœ์ข… ๋‹ต๋ณ€\u001b[39;00m\n", "\u001b[31mAttributeError\u001b[39m: module 'ollama' has no attribute 'chat'" ] } ], "source": [ "import ollama, json\n", "\n", "# Tool ํ•จ์ˆ˜๋“ค\n", "def get_weather(city):\n", " return {\"temp\": 12, \"condition\": \"๋น„\"}\n", "\n", "def need_umbrella(condition):\n", " return {\"result\": condition in [\"๋น„\", \"๋ˆˆ\"]}\n", "\n", "def calculate(expression):\n", " return {\"result\": eval(expression)} # ์‹ค์ œ๋ก  ์•ˆ์ „ํ•œ ํŒŒ์„œ ์‚ฌ์šฉ\n", "\n", "# Tool ์Šคํ‚ค๋งˆ ๋ชฉ๋ก\n", "tools = [\n", " {\"type\": \"function\", \"function\": {\n", " \"name\": \"get_weather\",\n", " \"description\": \"๋„์‹œ ๋‚ ์”จ ์กฐํšŒ\",\n", " \"parameters\": {\"type\": \"object\",\n", " \"properties\": {\"city\": {\"type\": \"string\"}},\n", " \"required\": [\"city\"]}}},\n", " {\"type\": \"function\", \"function\": {\n", " \"name\": \"need_umbrella\",\n", " \"description\": \"๋‚ ์”จ ์ƒํƒœ๋กœ ์šฐ์‚ฐ ํ•„์š” ์—ฌ๋ถ€ ํŒ๋‹จ\",\n", " \"parameters\": {\"type\": \"object\",\n", " \"properties\": {\"condition\": {\"type\": \"string\"}},\n", " \"required\": [\"condition\"]}}},\n", "]\n", "\n", "tool_map = {\n", " \"get_weather\": get_weather,\n", " \"need_umbrella\": need_umbrella,\n", "}\n", "\n", "messages = [{\"role\": \"user\", \"content\": \"์„œ์šธ ๋‚ ์”จ ๋ณด๊ณ  ์šฐ์‚ฐ์ด ํ•„์š”ํ•œ์ง€ ์•Œ๋ ค์ค˜\"}]\n", "\n", "# โ”€โ”€ ์ž๋™ ๋ฃจํ”„: Tool ํ˜ธ์ถœ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต โ”€โ”€\n", "while True:\n", " response = ollama.chat(model=\"qwen2.5\", messages=messages, tools=tools)\n", "\n", " if not response.message.tool_calls:\n", " # Tool ํ˜ธ์ถœ ์—†์Œ = ์ตœ์ข… ๋‹ต๋ณ€\n", " print(response.message.content)\n", " break\n", "\n", " messages.append(response.message)\n", "\n", " for tc in response.message.tool_calls:\n", " name = tc.function.name\n", " args = tc.function.arguments\n", " result = tool_map[name](**args)\n", " print(f\"[Tool] {name}({args}) โ†’ {result}\")\n", "\n", " messages.append({\n", " \"role\": \"tool\",\n", " \"content\": json.dumps(result, ensure_ascii=False),\n", " })" ] }, { "cell_type": "code", "execution_count": null, "id": "1b261a1d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "์‘๋‹ต: ํŒŒ์ด์ฌ์˜ ์ฃผ์š” ์žฅ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:\n", "\n", "1. **๊ฐ„๊ฒฐ์„ฑ**: ํŒŒ์ด์ฌ ์ฝ”๋“œ๋Š” ๋งค์šฐ ๊ฐ„๊ฒฐํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด, ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” C++๋‚˜ Java์™€ ๋น„๊ตํ•ด๋ณด๋”๋ผ๋„ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ›จ์”ฌ ์งง๊ณ  ์ง๊ด€์ ์ธ ๋ฌธ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n", "\n", "2. **์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ์ง€์›**: ํŒŒ์ด์ฌ์€ ๋งค์šฐ ํฐ ๊ทœ๋ชจ์˜ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ด ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋„๊ตฌ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ด์œ ๋กœ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๊ฐ€ ์‹œ์ž‘ํ•˜๊ธฐ ์‰ฝ๊ณ , ๋ฌธ์ œ ํ•ด๊ฒฐ์ด๋‚˜ ํŠน์ • ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ๋„์›€์„ ์–ป๋Š”๋ฐ๋„ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.\n", "\n", "3. **๋‹ค๋ชฉ์ ์„ฑ**: ํŒŒ์ด์ฌ์€ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์›น ๊ฐœ๋ฐœ, ๋ฐ์ดํ„ฐ ๊ณผํ•™, ๋จธ์‹  ๋Ÿฌ๋‹, ๊ทธ๋ž˜ํ”ฝ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n", "\n", "์ด ์™ธ์—๋„ ํ˜ธํ™˜์„ฑ๊ณผ ํ”Œ๋žซํผ ๋…๋ฆฝ์„ฑ, ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์šฐ๊ธฐ ์‰ฝ๊ณ  ์œ ์ง€ ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฌ์šด ํŠน์ง•๋“ค๋„ ํŒŒ์ด์ฌ์˜ ์žฅ์ ์œผ๋กœ ๊ผฝํž™๋‹ˆ๋‹ค.\n" ] } ], "source": [ "import requests\n", "\n", "# โ‘  Chat Completion - ๊ธฐ๋ณธ ํ˜ธ์ถœ\n", "response = requests.post(\n", " \"http://localhost:11434/api/chat\",\n", " json={\n", " \"model\": \"qwen2.5\",\n", " \"messages\": [\n", " {\"role\": \"system\", \"content\": \"๋‹น์‹ ์€ ์นœ์ ˆํ•œ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.\"},\n", " {\"role\": \"user\", \"content\": \"ํŒŒ์ด์ฌ์˜ ์žฅ์ ์„ 3๊ฐ€์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”.\"},\n", " ],\n", " \"stream\": False,\n", " },\n", ")\n", "\n", "data = response.json()\n", "print(\"์‘๋‹ต:\", data[\"message\"][\"content\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "b2b03ad1", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'ollama' has no attribute 'embed'", "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[35]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m response = ollama.embed(\n\u001b[32m 2\u001b[39m model=\u001b[33m'qwen3-embedding:4b'\u001b[39m,\n\u001b[32m 3\u001b[39m input=\u001b[33m'The sky is blue because of Rayleigh scattering'\u001b[39m,\n\u001b[32m 4\u001b[39m )\n", "\u001b[31mAttributeError\u001b[39m: module 'ollama' has no attribute 'embed'" ] } ], "source": [ "\n", "\n", "response = ollama.embed(\n", " model='qwen3-embedding:4b',\n", " input='The sky is blue because of Rayleigh scattering',\n", ")\n", "print(response.embeddings)" ] }, { "cell_type": "code", "execution_count": null, "id": "f3b6034b", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mRunning cells with 'Python 3.12.3' requires the ipykernel package.\n", "\u001b[1;31mInstall 'ipykernel' into the Python environment. \n", "\u001b[1;31mCommand: '/bin/python3 -m pip install ipykernel -U --user --force-reinstall'" ] } ], "source": [ "!pip install numpy" ] }, { "cell_type": "code", "execution_count": 38, "id": "97c6625f", "metadata": {}, "outputs": [], "source": [ "# ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„\n", "from numpy import dot\n", "from numpy.linalg import norm\n", "\n", "def cosine_similarity(a, b):\n", " return dot(a, b) / (norm(a) * norm(b))" ] }, { "cell_type": "code", "execution_count": 1, "id": "e85ee4e5", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'ollama' is not defined", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์œ ์‚ฌํ•œ๊ฐ€?\u001b[39;00m\n\u001b[32m 2\u001b[39m s1 = \u001b[33m\"ํŒŒ์ด์ฌ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค\"\u001b[39m\n\u001b[32m 3\u001b[39m s2 = \u001b[33m\"Python์€ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์ด๋‹ค.\"\u001b[39m\n\u001b[32m 4\u001b[39m \n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m e1 = ollama.embed(model=\u001b[33m'qwen3-embedding:4b'\u001b[39m, input=s1).embedding[\u001b[32m0\u001b[39m]\n\u001b[32m 6\u001b[39m e2 = ollama.embed(model=\u001b[33m'qwen3-embedding:4b'\u001b[39m, input=s2).embedding[\u001b[32m0\u001b[39m]\n\u001b[32m 7\u001b[39m \n\u001b[32m 8\u001b[39m print(cosine_similarity(e1,e2))\n", "\u001b[31mNameError\u001b[39m: name 'ollama' is not defined" ] } ], "source": [ "# ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์œ ์‚ฌํ•œ๊ฐ€?\n", "s1 = \"ํŒŒ์ด์ฌ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค\"\n", "s2 = \"Python์€ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์ด๋‹ค.\"\n", "\n", "e1 = ollama.embed(model='qwen3-embedding:4b', input=s1).embedding[0]\n", "e2 = ollama.embed(model='qwen3-embedding:4b', input=s2).embedding[0]\n", "\n", "print(cosine_similarity(e1,e2))" ] }, { "cell_type": "code", "execution_count": null, "id": "487edd1a", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mRunning cells with 'Python 3.12.3' requires the ipykernel package.\n", "\u001b[1;31mCreate a Python Environment with the required packages." ] } ], "source": [ "documents = [\n", " \"ํŒŒ์ด์ฌ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค\",\n", " \"์ถ•๊ตฌ๋Š” ์„ธ๊ณ„์ ์œผ๋กœ ์ธ๊ธฐ์žˆ๋Š” ์Šคํฌ์ธ ์ด๋‹ค.\",\n", " \"์ธ๊ณต์ง€๋Šฅ์€ ๋จธ์‹ ๋Ÿฌ๋‹์„ ํ™œ์šฉํ•œ๋‹ค\",\n", " \"์„œ์šธ์€ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„์ด๋‹ค\",\n", "]\n", "\n", "doc_vectors =[]\n", "\n", "for doc in documnets:\n", " vector = ollama.embed(model='qwem3-embedding:4b', input=doc).embedding[0]\n", " doc_vectors.append(vector)\n", "\n", "query = \"AI ๊ธฐ์ˆ ์ด๋ž€?\"\n", "query_vector = ollama.embed(model='qwem3-embedding:4b', input=query).embedding[0]\n", "\n", "scores = []\n", "for doc, vec in zip(documents, doc_vectors):\n", " score = cosine_similarity(query_vector,vec)\n", " scores.append((doc, score))\n", "\n", "scores.sort(key=lambda x:x[1], reversed=True)\n", "\n", "for doc, score in scores:\n", " print(doc, score)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "892f1810", "metadata": {}, "outputs": [], "source": [ "context = scores[0][0]\n", "\n", "prompt = f\"\"\"\n", "๋ฌธ์„œ : \n", "{context}\n", "\n", "์งˆ๋ฌธ : \n", "{query}\n", "\"\"\"\n", "\n", "response = chat(model=\"qwen2.5:7b\", messages=[{\"role\":\"user\", \"content\":prompt}])\n", "print(response.message.content)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.12.3)", "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.3" } }, "nbformat": 4, "nbformat_minor": 5 }