|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "intro" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Fine-tuning GPT-3.5 for Agent Training\n", |
| 10 | + "\n", |
| 11 | + "This notebook demonstrates fine-tuning a GPT-3.5 model for use in an agent that uses a retrieval tool." |
| 12 | + ] |
| 13 | + }, |
3 | 14 | { |
4 | 15 | "cell_type": "code", |
5 | 16 | "execution_count": 1, |
|
8 | 19 | }, |
9 | 20 | "outputs": [], |
10 | 21 | "source": [ |
11 | | - "!pip install -qU \\\n", |
12 | | - " datasets==2.14.4 \\\n", |
13 | | - " langchain==0.0.274 \\\n", |
14 | | - " pinecone>=5.0.0 \\\n", |
15 | | - " openai==0.27.9" |
| 22 | + "!pip install -qU datasets==2.14.4 langchain==0.0.274 pinecone==5.0.0 openai==0.27.9\n", |
| 23 | + "\n", |
| 24 | + "import os\n", |
| 25 | + "from getpass import getpass\n", |
| 26 | + "from time import sleep\n", |
| 27 | + "\n", |
| 28 | + "import openai\n", |
| 29 | + "import requests\n", |
| 30 | + "from chains import VectorDBChain\n", |
| 31 | + "from datasets import load_dataset\n", |
| 32 | + "from langchain.agents import AgentType, Tool, initialize_agent\n", |
| 33 | + "from langchain.chat_models import ChatOpenAI\n", |
| 34 | + "from langchain.memory import ConversationBufferWindowMemory\n", |
| 35 | + "\n", |
| 36 | + "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or \"YOUR_API_KEY\"\n", |
| 37 | + "openai.api_key = os.environ[\"OPENAI_API_KEY\"]" |
16 | 38 | ] |
17 | 39 | }, |
18 | 40 | { |
|
41 | 63 | } |
42 | 64 | ], |
43 | 65 | "source": [ |
44 | | - "from datasets import load_dataset\n", |
45 | | - "\n", |
46 | 66 | "data = load_dataset(\"jamescalam/agent-conversations-retrieval-tool\", split=\"train\")\n", |
47 | 67 | "data" |
48 | 68 | ] |
|
186 | 206 | } |
187 | 207 | ], |
188 | 208 | "source": [ |
189 | | - "import os\n", |
190 | | - "import openai\n", |
191 | | - "\n", |
192 | | - "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or \"YOUR_API_KEY\"\n", |
193 | | - "openai.api_key = os.environ[\"OPENAI_API_KEY\"]\n", |
194 | | - "\n", |
195 | 209 | "res = openai.File.create(file=open(\"conversations.jsonl\", \"r\"), purpose=\"fine-tune\")\n", |
196 | 210 | "res" |
197 | 211 | ] |
|
460 | 474 | } |
461 | 475 | ], |
462 | 476 | "source": [ |
463 | | - "from time import sleep\n", |
464 | | - "\n", |
465 | 477 | "while True:\n", |
466 | 478 | " res = openai.FineTuningJob.retrieve(job_id)\n", |
467 | | - " if res[\"finished_at\"] != None:\n", |
| 479 | + " if res[\"finished_at\"] is not None:\n", |
468 | 480 | " break\n", |
469 | 481 | " else:\n", |
470 | 482 | " print(\".\", end=\"\")\n", |
|
540 | 552 | }, |
541 | 553 | "outputs": [], |
542 | 554 | "source": [ |
543 | | - "import requests\n", |
544 | | - "\n", |
545 | 555 | "res = requests.get(\n", |
546 | 556 | " \"https://raw.githubusercontent.com/pinecone-io/examples/master/learn/generation/openai/fine-tuning/gpt-3.5-agent-training/chains.py\"\n", |
547 | 557 | ")\n", |
|
557 | 567 | }, |
558 | 568 | "outputs": [], |
559 | 569 | "source": [ |
560 | | - "from getpass import getpass\n", |
561 | | - "from langchain.agents import Tool\n", |
562 | | - "from langchain.chat_models import ChatOpenAI\n", |
563 | | - "from langchain.memory import ConversationBufferWindowMemory\n", |
564 | | - "from chains import VectorDBChain\n", |
565 | | - "\n", |
566 | 570 | "llm = ChatOpenAI(temperature=0.5, model_name=ft_model)\n", |
567 | 571 | "\n", |
568 | 572 | "memory = ConversationBufferWindowMemory(\n", |
|
591 | 595 | }, |
592 | 596 | "outputs": [], |
593 | 597 | "source": [ |
594 | | - "from langchain.agents import AgentType, initialize_agent\n", |
595 | | - "\n", |
596 | 598 | "agent = initialize_agent(\n", |
597 | 599 | " agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,\n", |
598 | 600 | " tools=[vdb_tool],\n", |
|
0 commit comments