├── langchain-tutorials ├── README.md ├── LangChain_Chatbot_with_Pinecone │ ├── requirements.txt │ ├── utils.py │ └── main.py ├── OpenAI_Whisper_Tutorial │ ├── Whisper_Transcribe_YouTube_Video.ipynb │ └── Whisper_And_ChatGPT_API_Summary_Audio_Tutorial.ipynb ├── LangChain_OpenAI_Proxy │ └── LangChain_With_OpenAI_Proxy_Invoke.ipynb ├── LangChain_HDBSCAN_Clustering │ └── Using_OpenAI__LangChain_And_HDBSCAN_Clustering_Documents.ipynb ├── LangChain_Database │ └── Langchain_Agents_SQL_Database_Agent.ipynb └── LangChain_Base │ └── LangChain_Prompt_Templates.ipynb ├── chromadb_tutorials ├── linux_doc │ ├── CentOS常用命令.pdf │ └── Training GPT.pdf └── pets │ ├── Health Care for Pets.txt │ ├── Training and Behaviour of Pets.txt │ ├── The Emotional Bond Between Humans and Pets.txt │ ├── Different Types of Pet Animals.txt │ └── Nutrition Needs of Pet Animals.txt ├── face-recognition-dlib └── README.md ├── README.md ├── clip-search └── README.md ├── embedding └── Generate_Examples_for_Embedding_Training.ipynb ├── fine-tuned-llm-trainer └── How_to_Fine_Tune_and_Train_LLMs_With_FAST_GPT_LLM_Trainer.ipynb ├── fine-tune-code-llama └── fine_tune_code_llama.ipynb └── gpt-author └── gpt_author_v2.ipynb /langchain-tutorials/README.md: -------------------------------------------------------------------------------- 1 | # langchain-tutorials -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_Chatbot_with_Pinecone/requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | streamlit_chat 3 | langchain 4 | sentence_transformers 5 | -------------------------------------------------------------------------------- /chromadb_tutorials/linux_doc/CentOS常用命令.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Crossme0809/frenzyTechAI/HEAD/chromadb_tutorials/linux_doc/CentOS常用命令.pdf -------------------------------------------------------------------------------- /chromadb_tutorials/linux_doc/Training GPT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Crossme0809/frenzyTechAI/HEAD/chromadb_tutorials/linux_doc/Training GPT.pdf -------------------------------------------------------------------------------- /face-recognition-dlib/README.md: -------------------------------------------------------------------------------- 1 | 2 | # 基于OpenCV和Dlib的深度学习人脸识别技术实践与应用 3 | [https://github.com/Crossme0809/face-recognition-dlib](https://github.com/Crossme0809/face-recognition-dlib) 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 【技术狂潮AI】 代码库 2 | * 本仓库主要收录微信公众号【技术狂潮AI】上涉及的所有案例代码仓库。
3 | 4 | -------------------------------------------------------------------------------- /clip-search/README.md: -------------------------------------------------------------------------------- 1 | # CLIP Search 2 | * [使用OpenAI CLIP进行文本到图像和图像到图像的搜索](https://mp.weixin.qq.com/s?__biz=MzkyMTQ5MjYyMg==&mid=2247495123&idx=1&sn=c2932fc2a415a71a2ca60d3b56a651f0&chksm=c1806032f6f7e924035c09d856d052bd60b9c1357ae7bb91e96514c00f583a15310a07b59881&token=260081155&lang=zh_CN#rd) 3 | 4 | -------------------------------------------------------------------------------- /chromadb_tutorials/pets/Health Care for Pets.txt: -------------------------------------------------------------------------------- 1 | Routine health care is crucial for pets to live long, happy lives. Regular vet check-ups help catch potential issues early and keep vaccinations up to date. Dental care is also essential to prevent diseases in pets, especially in dogs and cats. Regular grooming, parasite control, and weight management are other important aspects of pet health care. -------------------------------------------------------------------------------- /chromadb_tutorials/pets/Training and Behaviour of Pets.txt: -------------------------------------------------------------------------------- 1 | Training is essential for a harmonious life with pets, particularly for dogs. It helps pets understand their boundaries and makes cohabitation easier for both pets and owners. Training should be based on positive reinforcement. Understanding pet behavior is also important, as changes in behavior can often be a sign of underlying health issues. -------------------------------------------------------------------------------- /chromadb_tutorials/pets/The Emotional Bond Between Humans and Pets.txt: -------------------------------------------------------------------------------- 1 | Pets offer more than just companionship; they provide emotional support, reduce stress, and can even help their owners lead healthier lives. The bond between pets and their owners is strong, and many people consider their pets as part of the family. This bond can be especially important in times of personal or societal stress, providing comfort and consistency. -------------------------------------------------------------------------------- /chromadb_tutorials/pets/Different Types of Pet Animals.txt: -------------------------------------------------------------------------------- 1 | Pet animals come in all shapes and sizes, each suited to different lifestyles and home environments. Dogs and cats are the most common, known for their companionship and unique personalities. Small mammals like hamsters, guinea pigs, and rabbits are often chosen for their low maintenance needs. Birds offer beauty and song, and reptiles like turtles and lizards can make intriguing pets. Even fish, with their calming presence, can be wonderful pets. 2 | -------------------------------------------------------------------------------- /chromadb_tutorials/pets/Nutrition Needs of Pet Animals.txt: -------------------------------------------------------------------------------- 1 | Proper nutrition is vital for the health and wellbeing of pets. Dogs and cats require a balanced diet that includes proteins, carbohydrates, and fats. Some may even have specific dietary needs based on their breed or age. Birds typically thrive on a diet of seeds, fruits, and vegetables, while reptiles have diverse diets ranging from live insects to fresh produce. Fish diets depend greatly on the species, with some needing live food and others subsisting on flakes or pellets. -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_Chatbot_with_Pinecone/utils.py: -------------------------------------------------------------------------------- 1 | from sentence_transformers import SentenceTransformer 2 | import pinecone 3 | import openai 4 | import streamlit as st 5 | openai.api_key = "" 6 | model = SentenceTransformer('all-MiniLM-L6-v2') 7 | 8 | pinecone.init(api_key='', environment='us-east-1-aws') 9 | index = pinecone.Index('langchain-chatbot') 10 | 11 | def find_match(input): 12 | input_em = model.encode(input).tolist() 13 | result = index.query(input_em, top_k=2, includeMetadata=True) 14 | return result['matches'][0]['metadata']['text']+"\n"+result['matches'][1]['metadata']['text'] 15 | 16 | def query_refiner(conversation, query): 17 | 18 | response = openai.Completion.create( 19 | model="text-davinci-003", 20 | prompt=f"给出以下用户查询和对话记录,制定一个最相关的问题,从知识库中为用户提供一个答案.\n\n对话记录: \n{conversation}\n\n用户查询: {query}\n\n优化查询:", 21 | temperature=0.7, 22 | max_tokens=256, 23 | top_p=1, 24 | frequency_penalty=0, 25 | presence_penalty=0 26 | ) 27 | return response['choices'][0]['text'] 28 | 29 | def get_conversation_string(): 30 | conversation_string = "" 31 | for i in range(len(st.session_state['responses'])-1): 32 | 33 | conversation_string += "Human: "+st.session_state['requests'][i] + "\n" 34 | conversation_string += "Bot: "+ st.session_state['responses'][i+1] + "\n" 35 | return conversation_string 36 | -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_Chatbot_with_Pinecone/main.py: -------------------------------------------------------------------------------- 1 | from langchain.chat_models import ChatOpenAI 2 | from langchain.chains import ConversationChain 3 | from langchain.chains.conversation.memory import ConversationBufferWindowMemory 4 | from langchain.prompts import ( 5 | SystemMessagePromptTemplate, 6 | HumanMessagePromptTemplate, 7 | ChatPromptTemplate, 8 | MessagesPlaceholder 9 | ) 10 | import streamlit as st 11 | from streamlit_chat import message 12 | from utils import * 13 | 14 | st.subheader("使用Langchain、ChatGPT、Pinecone和Streamlit构建的的聊天机器人") 15 | 16 | if 'responses' not in st.session_state: 17 | st.session_state['responses'] = ["How can I assist you?"] 18 | 19 | if 'requests' not in st.session_state: 20 | st.session_state['requests'] = [] 21 | 22 | llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key="") 23 | 24 | if 'buffer_memory' not in st.session_state: 25 | st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True) 26 | 27 | 28 | system_msg_template = SystemMessagePromptTemplate.from_template(template="""利用所提供的上下文信息,尽可能真实地回答问题、 29 | 如果答案不在下面的文字中,请说 '我不知道'""") 30 | 31 | 32 | human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}") 33 | 34 | prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template]) 35 | 36 | conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True) 37 | 38 | 39 | 40 | 41 | # container for chat history 42 | response_container = st.container() 43 | # container for text box 44 | textcontainer = st.container() 45 | 46 | 47 | with textcontainer: 48 | query = st.text_input("Query: ", key="input") 49 | if query: 50 | with st.spinner("typing..."): 51 | conversation_string = get_conversation_string() 52 | # st.code(conversation_string) 53 | refined_query = query_refiner(conversation_string, query) 54 | st.subheader("Refined Query:") 55 | st.write(refined_query) 56 | context = find_match(refined_query) 57 | # print(context) 58 | response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{query}") 59 | st.session_state.requests.append(query) 60 | st.session_state.responses.append(response) 61 | with response_container: 62 | if st.session_state['responses']: 63 | 64 | for i in range(len(st.session_state['responses'])): 65 | message(st.session_state['responses'][i],key=str(i)) 66 | if i < len(st.session_state['requests']): 67 | message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user') 68 | 69 | 70 | -------------------------------------------------------------------------------- /embedding/Generate_Examples_for_Embedding_Training.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "include_colab_link": true 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "view-in-github", 22 | "colab_type": "text" 23 | }, 24 | "source": [ 25 | "\"Open" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": { 32 | "id": "zxNSe5AdIu6X" 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "!pip install langchain\n", 37 | "!pip install openai" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "source": [ 43 | "import openai\n", 44 | "from langchain.prompts.chat import (\n", 45 | " ChatPromptTemplate,\n", 46 | " HumanMessagePromptTemplate,\n", 47 | " SystemMessagePromptTemplate,\n", 48 | ")\n", 49 | "from langchain.schema import HumanMessage, SystemMessage\n", 50 | "from langchain.chat_models import ChatOpenAI" 51 | ], 52 | "metadata": { 53 | "id": "RvznLBHvMkpG" 54 | }, 55 | "execution_count": null, 56 | "outputs": [] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "source": [ 61 | "# 使用GPT-4生成高级任务列表\n", 62 | "# 别忘了输入你的API key\n", 63 | "\n", 64 | "llm = ChatOpenAI(api_key=\"ENTER YOUR KEY HERE\",\n", 65 | " model_name=\"gpt-4\")\n", 66 | "messages = [\n", 67 | " HumanMessage(\n", 68 | " content=\n", 69 | "\"\"\"\n", 70 | "头脑风暴10个潜在有用的文本检索任务。\n", 71 | "这里有几个例子供您参考:\n", 72 | "- 提供科学声明作为查询,检索有助于验证或反驳声明的文档。\n", 73 | "- 搜索有关儿童营养的FAQ式查询的文档。\n", 74 | "请遵守以下准则:\n", 75 | "- 指定查询是什么,以及所需的文档是什么。\n", 76 | "- 每个检索任务应该涵盖广泛的查询,并且不应该过于具体。\n", 77 | "您的输出应该始终是仅包含10个元素的python字符串列表,每个元素在一句话中对应一个不同的检索任务。不要解释自己或输出其他任何内容。要有创意!\n", 78 | "\"\"\"\n", 79 | " )\n", 80 | "]\n", 81 | "result = llm(messages)\n", 82 | "tasks = eval(result.content)\n", 83 | "tasks" 84 | ], 85 | "metadata": { 86 | "id": "cqRqNFilMu5b" 87 | }, 88 | "execution_count": null, 89 | "outputs": [] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "source": [ 94 | "# 生成一个JSON格式的示例任务\n", 95 | "\n", 96 | "# 您可以根据您的要求(提示长度、提示样式、输出文档长度、语言等)更改此模板\n", 97 | "template = (\n", 98 | " \"\"\"\n", 99 | "您已被分配了一个检索任务:{task}\n", 100 | "您的任务是以JSON格式为此任务编写一个文本检索示例。JSON对象必须包含以下键:\n", 101 | "-\"user_query\":一个字符串,由检索任务指定的随机用户搜索查询。\n", 102 | "-\"positive_document\":一个字符串,用于用户查询的相关文档。\n", 103 | "-\"hard_negative_document\":一个字符串,一个只与查询相关的硬否定文档。\n", 104 | "请遵守以下准则:\n", 105 | "-“user_query”应该是10-20个单词,清晰,主题多样。\n", 106 | "-所有文件的长度应至少为150字。\n", 107 | "-查询和文件都应该是英文的。\n", 108 | "你的输出必须始终是JSON对象,不要解释自己或输出其他任何东西。要有创意!\n", 109 | " \"\"\"\n", 110 | ")\n", 111 | "\n", 112 | "prompt = ChatPromptTemplate.from_messages([\n", 113 | " (HumanMessagePromptTemplate.from_template(template))\n", 114 | "])\n", 115 | "\n", 116 | "# 您可以将这部分放入循环中以创建更多示例\n", 117 | "result = llm(prompt.format_prompt(task=tasks[0]).to_messages())\n", 118 | "result.content\n" 119 | ], 120 | "metadata": { 121 | "id": "3se3r85qXGV3" 122 | }, 123 | "execution_count": null, 124 | "outputs": [] 125 | } 126 | ] 127 | } -------------------------------------------------------------------------------- /langchain-tutorials/OpenAI_Whisper_Tutorial/Whisper_Transcribe_YouTube_Video.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNtAG23ZXN4bjDwqkS180xF", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# **Whisper 转录 YouTube *视频***" 33 | ], 34 | "metadata": { 35 | "id": "Ai6GxDDor_tf" 36 | } 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "source": [ 41 | "# **安装pytube库**\n", 42 | "\n", 43 | "---\n", 44 | "\n", 45 | "接下来我们将使用 Whisper 转录 YouTube 视频。我们将使用 Python 的“pytube”库将音频转换为MP4。文件\n", 46 | "\n", 47 | "\n", 48 | "```\n", 49 | "pytube repo:https://github.com/pytube/pytube。\n", 50 | "```\n", 51 | "\n", 52 | "\n", 53 | "*首先,我们需要安装* Pytube 库。您可以通过在终端中运行以下命令来执行此操作:" 54 | ], 55 | "metadata": { 56 | "id": "NGjXLVgNsUpc" 57 | } 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 1, 62 | "metadata": { 63 | "colab": { 64 | "base_uri": "https://localhost:8080/" 65 | }, 66 | "id": "OQ5nLh1Xro1r", 67 | "outputId": "487c37f8-af0c-4469-9795-03e457f4dcf6" 68 | }, 69 | "outputs": [ 70 | { 71 | "output_type": "stream", 72 | "name": "stdout", 73 | "text": [ 74 | "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", 75 | "Collecting pytube\n", 76 | " Downloading pytube-15.0.0-py3-none-any.whl (57 kB)\n", 77 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.6/57.6 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 78 | "\u001b[?25hInstalling collected packages: pytube\n", 79 | "Successfully installed pytube-15.0.0\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "!pip install --upgrade pytube" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "source": [ 90 | "# **下载音频MP4文件**\n", 91 | "\n", 92 | "接下来,我们需要导入 pytube,提供 YouTube 视频的链接,并将音频转换为MP4:" 93 | ], 94 | "metadata": { 95 | "id": "U2niFIRts8d2" 96 | } 97 | }, 98 | { 99 | "cell_type": "code", 100 | "source": [ 101 | "# 导入Pytube库\n", 102 | "import pytube\n", 103 | "\n", 104 | "#加载YouTube链接\n", 105 | "video = \"https://www.youtube.com/watch?v=x7X9w_GIm1s\"\n", 106 | "\n", 107 | "# 转换和下载为MP4文件\n", 108 | "data = pytube.YouTube(video)\n", 109 | "audio = data.streams.get_audio_only()\n", 110 | "audio.download()" 111 | ], 112 | "metadata": { 113 | "id": "m8MZqXfHtII-" 114 | }, 115 | "execution_count": null, 116 | "outputs": [] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "source": [ 121 | "# **安装 Whisper 库**" 122 | ], 123 | "metadata": { 124 | "id": "Hz_LnnLYtSqu" 125 | } 126 | }, 127 | { 128 | "cell_type": "code", 129 | "source": [ 130 | "!pip install git+https://github.com/openai/whisper.git -q" 131 | ], 132 | "metadata": { 133 | "id": "1jo9UdWItYLF" 134 | }, 135 | "execution_count": null, 136 | "outputs": [] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "source": [ 141 | "import whisper\n", 142 | "\n", 143 | "model = whisper.load_model(\"base\")\n", 144 | "text = model.transcribe(\"Python in 100 Seconds.mp4\")\n", 145 | "\n", 146 | "#printing the transcribe\n", 147 | "text['text']" 148 | ], 149 | "metadata": { 150 | "id": "G7hHNJCmtdLV" 151 | }, 152 | "execution_count": null, 153 | "outputs": [] 154 | } 155 | ] 156 | } 157 | -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_OpenAI_Proxy/LangChain_With_OpenAI_Proxy_Invoke.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNI0uPEexNHkz3D2d0Kyeov", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# **如何在本地环境使用 `LangChain` 调用 `OpenAI` 代理(免科学上网)**" 33 | ], 34 | "metadata": { 35 | "id": "1ZBFWoLuTjED" 36 | } 37 | }, 38 | { 39 | "cell_type": "code", 40 | "source": [ 41 | "!pip install openai langchain" 42 | ], 43 | "metadata": { 44 | "id": "miLXtr5VySOt" 45 | }, 46 | "execution_count": null, 47 | "outputs": [] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "source": [ 52 | "!pip install python-dotenv" 53 | ], 54 | "metadata": { 55 | "id": "HdcC36lJ3Tci" 56 | }, 57 | "execution_count": null, 58 | "outputs": [] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "source": [ 63 | "## **方式一:通过在 `.env` 文件中设置 `OPENAI_API_BASE` 环境变量**\n" 64 | ], 65 | "metadata": { 66 | "id": "-cpxaWAjSpPM" 67 | } 68 | }, 69 | { 70 | "cell_type": "code", 71 | "source": [ 72 | "import os\n", 73 | "from dotenv import load_dotenv\n", 74 | "from langchain.schema import HumanMessage\n", 75 | "from langchain.chat_models import ChatOpenAI\n", 76 | "\n", 77 | "# 加载.env文件中的环境变量\n", 78 | "load_dotenv()\n", 79 | "\n", 80 | "# 创建OpenAI实例\n", 81 | "llm = ChatOpenAI(\n", 82 | " model_name=\"gpt-3.5-turbo-0613\",\n", 83 | " max_tokens=1024,\n", 84 | " verbose=True\n", 85 | " )\n", 86 | "result = llm([HumanMessage(content=\"什么是LLM?\")])\n", 87 | "print(result.content)\n", 88 | "\n", 89 | "# 输出模型的参数信息可以看到已成功加载环境变量中OPENAI_API_BASE的值\n", 90 | "print(llm.json)\n" 91 | ], 92 | "metadata": { 93 | "colab": { 94 | "base_uri": "https://localhost:8080/" 95 | }, 96 | "id": "kEC6cavpPZBO", 97 | "outputId": "5d14d6f6-b5b0-4db6-978a-3a3705f62e4a" 98 | }, 99 | "execution_count": null, 100 | "outputs": [ 101 | { 102 | "output_type": "stream", 103 | "name": "stdout", 104 | "text": [ 105 | "LLM是法学硕士(Master of Laws)的缩写。它是一个专业学位,主要面向已经获得法学学士学位或者相关学科学士学位的学生。LLM的学位课程通常涵盖法学的各个领域,如国际法、商法、民法、刑法等。这个学位旨在深入研究法学领域的专业知识和技能,提供更高水平的法律教育和培训。LLM学位在提升法律职业发展、进入法律界的国际化环境、深化法学研究等方面具有重要作用。\n", 106 | ", model_name='gpt-3.5-turbo-0613', temperature=0.7, model_kwargs={}, openai_api_key='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', openai_api_base='https://gpt-proxy-3qe.pages.dev/api/v1', openai_organization='', openai_proxy='', request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=1024)>\n" 107 | ] 108 | } 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "source": [ 114 | "## **方式二:在初始化 `ChatOpenAI` 模型时指定 `openai_api_base` 参数**" 115 | ], 116 | "metadata": { 117 | "id": "Wi459Dj_TOhw" 118 | } 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": { 124 | "colab": { 125 | "base_uri": "https://localhost:8080/" 126 | }, 127 | "id": "Lih5m2o2u-cd", 128 | "outputId": "b9e75e21-9daf-4754-fbea-b437f4ba915a" 129 | }, 130 | "outputs": [ 131 | { 132 | "output_type": "stream", 133 | "name": "stdout", 134 | "text": [ 135 | "LLM是法学硕士(Master of Laws)的缩写。它是一种研究生法学学位,通常是为那些已经获得法学学士学位的人提供的进一步学习和专业发展的机会。LLM课程通常侧重于深入研究法律领域的特定主题,如国际法、商法、人权法等。LLM学位可以在全球各地的大学和法学院获得。\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "import os\n", 141 | "from dotenv import load_dotenv\n", 142 | "from langchain.schema import HumanMessage\n", 143 | "from langchain.chat_models import ChatOpenAI\n", 144 | "\n", 145 | "# 加载.env文件中的环境变量\n", 146 | "load_dotenv()\n", 147 | "\n", 148 | "# 创建OpenAI实例\n", 149 | "llm = ChatOpenAI(\n", 150 | " model_name=\"gpt-3.5-turbo-0613\",\n", 151 | " max_tokens=1024,\n", 152 | " verbose=True,\n", 153 | " openai_api_base=os.getenv(\"OPENAI_API_BASE\")\n", 154 | " )\n", 155 | "result = llm([HumanMessage(content=\"什么是LLM?\")])\n", 156 | "print(result.content)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "source": [ 162 | "## **基于`OpenAI Python SDK`调用`GPT`模型**" 163 | ], 164 | "metadata": { 165 | "id": "QXAy3JK8emEw" 166 | } 167 | }, 168 | { 169 | "cell_type": "code", 170 | "source": [ 171 | "import openai\n", 172 | "\n", 173 | "completion = openai.ChatCompletion.create(model=\"gpt-3.5-turbo-0613\", messages=[{\"role\": \"user\", \"content\": \"请介绍下Google的PaLM模型!\"}])\n", 174 | "print(completion.choices[0].message.content)" 175 | ], 176 | "metadata": { 177 | "colab": { 178 | "base_uri": "https://localhost:8080/" 179 | }, 180 | "id": "swUfRmguemM1", 181 | "outputId": "aef6f596-24e4-4120-ece1-466ef95286da" 182 | }, 183 | "execution_count": null, 184 | "outputs": [ 185 | { 186 | "output_type": "stream", 187 | "name": "stdout", 188 | "text": [ 189 | "PaLM(Pretraining and Language Model)是Google在2020年提出的一种自然语言处理模型。PaLM的基本思想是将预训练和微调两个步骤结合起来,以提高模型的性能。\n", 190 | "\n", 191 | "PaLM模型的预训练阶段包括两个部分:语言建模(Language Modeling)和掩码语言建模(Masked Language Modeling)。语言建模通过让模型预测给定上下文下的下一个单词或token来学习语言的统计规律。而掩码语言建模则是在给定上下文中,随机掩盖一部分token,然后让模型预测被掩盖的token。这两个预训练任务有助于模型学习到单词和上下文之间的关系。\n", 192 | "\n", 193 | "在预训练之后,PaLM模型会进行微调以适应特定的下游任务,比如问答、文本分类等。微调阶段通过在特定任务上的有监督学习来调整模型的参数,使其更好地适应任务要求。\n", 194 | "\n", 195 | "PaLM模型在实验中表现出了良好的性能,它在自然语言理解和生成任务上都取得了较好的结果。与其他一些模型相比,PaLM模型具有良好的通用性和可扩展性,可以适应不同类型的下游任务。此外,PaLM模型还具有较强的泛化能力和抗噪能力。\n", 196 | "\n", 197 | "总结起来,Google的PaLM模型通过预训练和微调相结合的方式,提高了模型的性能,在自然语言处理任务中取得了良好的结果。\n" 198 | ] 199 | } 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "source": [ 205 | "## **通过`Python`直接调用`OpenAI HTTP`请求查询`Key`余额**" 206 | ], 207 | "metadata": { 208 | "id": "iNMyGYKWls7x" 209 | } 210 | }, 211 | { 212 | "cell_type": "code", 213 | "source": [ 214 | "import requests\n", 215 | "\n", 216 | "url = \"https://openai.1rmb.tk/v1/chat/completions\"\n", 217 | "api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'\n", 218 | "\n", 219 | "headers = {\n", 220 | " 'Authorization': f'Bearer {api_key}',\n", 221 | " 'Content-Type': 'application/json'\n", 222 | "}\n", 223 | "\n", 224 | "payload = {\n", 225 | " \"model\": \"gpt-3.5-turbo\",\n", 226 | " \"messages\": [\n", 227 | " {\n", 228 | " \"role\": \"user\",\n", 229 | " \"content\": \"Chroma和Pinecone的区别是什么?\"\n", 230 | " }\n", 231 | " ]\n", 232 | "}\n", 233 | "\n", 234 | "try:\n", 235 | " response = requests.post(url, headers=headers, json=payload)\n", 236 | " response.raise_for_status() # 抛出异常,如果响应码不是200\n", 237 | " data = response.json()\n", 238 | " print(data)\n", 239 | "except requests.exceptions.RequestException as e:\n", 240 | " print(f\"请求错误: {e}\")\n" 241 | ], 242 | "metadata": { 243 | "id": "B0ihDXEDlZjW" 244 | }, 245 | "execution_count": null, 246 | "outputs": [] 247 | } 248 | ] 249 | } 250 | -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_HDBSCAN_Clustering/Using_OpenAI__LangChain_And_HDBSCAN_Clustering_Documents.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "source": [ 16 | "!python3.10 --version" 17 | ], 18 | "metadata": { 19 | "colab": { 20 | "base_uri": "https://localhost:8080/" 21 | }, 22 | "id": "GNgna6Mcr-f8", 23 | "outputId": "2d2c7f13-5254-4dd9-f34c-18a015879507" 24 | }, 25 | "execution_count": 5, 26 | "outputs": [ 27 | { 28 | "output_type": "stream", 29 | "name": "stdout", 30 | "text": [ 31 | "Python 3.10.12\n" 32 | ] 33 | } 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "source": [ 39 | "## **安装依赖**" 40 | ], 41 | "metadata": { 42 | "id": "7tqCm0t9sifX" 43 | } 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "%pip install -r requirements.txt" 49 | ], 50 | "metadata": { 51 | "id": "Cool57g7sVmF" 52 | }, 53 | "execution_count": null, 54 | "outputs": [] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 18, 59 | "metadata": { 60 | "colab": { 61 | "base_uri": "https://localhost:8080/" 62 | }, 63 | "id": "56denEvKvJZS", 64 | "outputId": "d5313828-35c8-4557-fcfb-66be1b7a2520" 65 | }, 66 | "outputs": [ 67 | { 68 | "output_type": "execute_result", 69 | "data": { 70 | "text/plain": [ 71 | "True" 72 | ] 73 | }, 74 | "metadata": {}, 75 | "execution_count": 18 76 | } 77 | ], 78 | "source": [ 79 | "import os\n", 80 | "\n", 81 | "import hdbscan\n", 82 | "import pandas as pd\n", 83 | "\n", 84 | "from langchain import LLMChain\n", 85 | "from langchain.chat_models import ChatOpenAI\n", 86 | "from langchain.embeddings.openai import OpenAIEmbeddings\n", 87 | "from langchain.prompts.chat import (\n", 88 | " ChatPromptTemplate,\n", 89 | " SystemMessagePromptTemplate,\n", 90 | " HumanMessagePromptTemplate,\n", 91 | ")\n", 92 | "from newsapi import NewsApiClient\n", 93 | "\n", 94 | "from dotenv import load_dotenv\n", 95 | "\n", 96 | "load_dotenv()" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "id": "v7mK8pQvvJZW" 103 | }, 104 | "source": [ 105 | "\n", 106 | "## 从相关来源获得10篇新闻报道(因为接口限速原因,这里演示10条)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 29, 112 | "metadata": { 113 | "id": "AdY8rR8YvJZY" 114 | }, 115 | "outputs": [], 116 | "source": [ 117 | "newsapi = NewsApiClient(api_key=os.getenv(\"NEWSAPI_API_KEY\"))\n", 118 | "sources_1 = [\n", 119 | " \"the-washington-post\",\n", 120 | " \"the-wall-street-journal\",\n", 121 | " \"business-insider\",\n", 122 | "]\n", 123 | "sources_2 = [\n", 124 | " \"google-news\"\n", 125 | "]" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 30, 131 | "metadata": { 132 | "id": "xVB0xTM1vJZZ" 133 | }, 134 | "outputs": [], 135 | "source": [ 136 | "recent_articles = []\n", 137 | "for source in [sources_1, sources_2]:\n", 138 | " recent_articles.extend(newsapi.get_everything(\n", 139 | " sources=\",\".join(source),\n", 140 | " language=\"zh\",\n", 141 | " page_size=50\n", 142 | " )[\"articles\"])" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "source": [ 148 | "print(recent_articles)" 149 | ], 150 | "metadata": { 151 | "id": "VU7xr4GNyzR0" 152 | }, 153 | "execution_count": null, 154 | "outputs": [] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": { 159 | "id": "tHvIf_CjvJZZ" 160 | }, 161 | "source": [ 162 | "## 生成新闻文章的嵌入向量" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 32, 168 | "metadata": { 169 | "id": "xXZjH9DsvJZa" 170 | }, 171 | "outputs": [], 172 | "source": [ 173 | "docs = [\n", 174 | " a[\"title\"] + \"\\n\\n\" + (a[\"description\"] or \"\")\n", 175 | " for a in recent_articles\n", 176 | "]" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 33, 182 | "metadata": { 183 | "id": "qNW0eB9uvJZa" 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "embeddings = OpenAIEmbeddings(chunk_size=1000).embed_documents(docs)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": { 193 | "id": "UDU4gksRvJZa" 194 | }, 195 | "source": [ 196 | "## 对文件进行聚类,并将结果存储在一个 dataframe 中" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 34, 202 | "metadata": { 203 | "id": "lUMMPJz1vJZb" 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "hdb = hdbscan.HDBSCAN(gen_min_span_tree=True, min_samples=3, min_cluster_size=3).fit(embeddings)" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 35, 213 | "metadata": { 214 | "id": "FjTBbPYFvJZb" 215 | }, 216 | "outputs": [], 217 | "source": [ 218 | "df = pd.DataFrame({\n", 219 | " \"title\": [article[\"title\"] for article in recent_articles],\n", 220 | " \"description\": [article[\"description\"] for article in recent_articles],\n", 221 | " \"cluster\": hdb.labels_,\n", 222 | "})\n", 223 | "df = df.query(\"cluster != -1\") # 删除不在群集中的文档" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": { 229 | "id": "mAAqX_yyvJZc" 230 | }, 231 | "source": [ 232 | "## 从每个聚类的文件中创建聚类主题" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 36, 238 | "metadata": { 239 | "id": "vqqrC-EZvJZc" 240 | }, 241 | "outputs": [], 242 | "source": [ 243 | "\n", 244 | "\n", 245 | "\n", 246 | "def get_prompt():\n", 247 | " system_template = \"你是一位记者专家。你要帮我为新闻文章写一个引人注目的主题标题。\"\n", 248 | " human_template = \"使用以下文章,写一个能概括这些文章的主题标题。\\n\\nARTICLES:{articles}\\n\\nTOPIC TITLE:\"\n", 249 | "\n", 250 | " return ChatPromptTemplate(\n", 251 | " messages=[\n", 252 | " SystemMessagePromptTemplate.from_template(system_template),\n", 253 | " HumanMessagePromptTemplate.from_template(human_template),\n", 254 | " ],\n", 255 | " input_variables=[\"articles\"],\n", 256 | " )\n", 257 | "\n", 258 | "\n", 259 | "articles_str = \"\\n\\n\".join(\n", 260 | " [article[\"title\"] + \"\\n\\n\" + (article[\"description\"] or \"\") for article in recent_articles]\n", 261 | ")\n", 262 | "\n", 263 | "prompt = get_prompt()\n", 264 | "\n", 265 | "for c in df.cluster.unique():\n", 266 | " chain = LLMChain(\n", 267 | " llm=ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo-0613\"), prompt=prompt, verbose=False\n", 268 | " )\n", 269 | " articles_str = \"\\n\".join(\n", 270 | " [\n", 271 | " f\"{article['title']}\\n{article['description']}\\n\"\n", 272 | " for article in df.query(f\"cluster == {c}\").to_dict(orient=\"records\")\n", 273 | " ]\n", 274 | " )\n", 275 | " result = chain.run(\n", 276 | " {\n", 277 | " \"articles\": articles_str,\n", 278 | " }\n", 279 | " )\n", 280 | " df.loc[df.cluster == c, \"topic_title\"] = result" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 41, 286 | "metadata": { 287 | "colab": { 288 | "base_uri": "https://localhost:8080/", 289 | "height": 355 290 | }, 291 | "id": "fV2OgrYQvJZc", 292 | "outputId": "40ec4025-89c1-4afc-e361-fac63510e6b4" 293 | }, 294 | "outputs": [ 295 | { 296 | "output_type": "stream", 297 | "name": "stdout", 298 | "text": [ 299 | "\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"\n" 300 | ] 301 | }, 302 | { 303 | "output_type": "display_data", 304 | "data": { 305 | "text/plain": [ 306 | " title \\\n", 307 | "11 龙舟竞渡华亭湖!松江区第十三届端午龙舟赛上午开赛_郊野 - 新民网 \n", 308 | "17 端午假期首日长三角铁路迎来客流高峰预计发送旅客340万人次 - 无锡新传媒 \n", 309 | "19 看演出体验非遗手工技艺北京西城端午活动精彩纷呈 - beijing.qianlong.com \n", 310 | "24 《颂·黄钟大吕》在国家大剧院音乐厅上演 - China Daily \n", 311 | "27 龙舟竞渡正端午长三角龙舟邀请赛在金山山阳镇举行_新民社会 - 新民网 \n", 312 | "\n", 313 | " description cluster \\\n", 314 | "11 龙舟竞渡华亭湖!松江区第十三届端午龙舟赛上午开赛_郊野  新民网 1 \n", 315 | "17 端午假期首日长三角铁路迎来客流高峰预计发送旅客340万人次  无锡新传媒 1 \n", 316 | "19 看演出体验非遗手工技艺北京西城端午活动精彩纷呈  beijing.qianlong.com 1 \n", 317 | "24 《颂·黄钟大吕》在国家大剧院音乐厅上演  China Daily 1 \n", 318 | "27 龙舟竞渡正端午长三角龙舟邀请赛在金山山阳镇举行_新民社会  新民网 1 \n", 319 | "\n", 320 | " topic_title \n", 321 | "11 \"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\" \n", 322 | "17 \"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\" \n", 323 | "19 \"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\" \n", 324 | "24 \"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\" \n", 325 | "27 \"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\" " 326 | ], 327 | "text/html": [ 328 | "\n", 329 | "
\n", 330 | "
\n", 331 | "
\n", 332 | "\n", 345 | "\n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | "
titledescriptionclustertopic_title
11龙舟竞渡华亭湖!松江区第十三届端午龙舟赛上午开赛_郊野 - 新民网龙舟竞渡华亭湖!松江区第十三届端午龙舟赛上午开赛_郊野  新民网1\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"
17端午假期首日长三角铁路迎来客流高峰预计发送旅客340万人次 - 无锡新传媒端午假期首日长三角铁路迎来客流高峰预计发送旅客340万人次  无锡新传媒1\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"
19看演出体验非遗手工技艺北京西城端午活动精彩纷呈 - beijing.qianlong.com看演出体验非遗手工技艺北京西城端午活动精彩纷呈  beijing.qianlong.com1\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"
24《颂·黄钟大吕》在国家大剧院音乐厅上演 - China Daily《颂·黄钟大吕》在国家大剧院音乐厅上演  China Daily1\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"
27龙舟竞渡正端午长三角龙舟邀请赛在金山山阳镇举行_新民社会 - 新民网龙舟竞渡正端午长三角龙舟邀请赛在金山山阳镇举行_新民社会  新民网1\"粽情端午:龙舟竞渡、非遗手工技艺和粽子包制活动丰富多彩\"
\n", 393 | "
\n", 394 | " \n", 404 | " \n", 405 | " \n", 442 | "\n", 443 | " \n", 467 | "
\n", 468 | "
\n", 469 | " " 470 | ] 471 | }, 472 | "metadata": {} 473 | } 474 | ], 475 | "source": [ 476 | "c = 1\n", 477 | "with pd.option_context(\"display.max_colwidth\", None):\n", 478 | " print(df.query(f\"cluster == {c}\").topic_title.values[0])\n", 479 | " display(df.query(f\"cluster == {c}\").head())" 480 | ] 481 | } 482 | ], 483 | "metadata": { 484 | "kernelspec": { 485 | "display_name": ".venv", 486 | "language": "python", 487 | "name": "python3" 488 | }, 489 | "language_info": { 490 | "codemirror_mode": { 491 | "name": "ipython", 492 | "version": 3 493 | }, 494 | "file_extension": ".py", 495 | "mimetype": "text/x-python", 496 | "name": "python", 497 | "nbconvert_exporter": "python", 498 | "pygments_lexer": "ipython3", 499 | "version": "3.10.4" 500 | }, 501 | "orig_nbformat": 4, 502 | "colab": { 503 | "provenance": [], 504 | "include_colab_link": true 505 | } 506 | }, 507 | "nbformat": 4, 508 | "nbformat_minor": 0 509 | } -------------------------------------------------------------------------------- /fine-tuned-llm-trainer/How_to_Fine_Tune_and_Train_LLMs_With_FAST_GPT_LLM_Trainer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "#**如何使用您自己的数据轻松快速地微调和培训LLM - GPT-LLM-Trainer**\n", 17 | "##**描述你的模型**\n", 18 | "我们通过尝试一种新的方法,可以轻松地根据你输入的任务描述来构建特定任务的模型。首先,尽可能的使用GPU,可以在Google Colab中设置可用的 GPU,然后创建模型,只需要在提示中描述您想要构建的模型。 具有描述性且清晰。你可以通过更改 `Hyperparameters` 单元格中的 `model_name` 来更改要微调的模型。" 19 | ], 20 | "metadata": { 21 | "id": "wM8MRkf8Dr94" 22 | } 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "source": [ 27 | "#**数据生成步骤**" 28 | ], 29 | "metadata": { 30 | "id": "Way3_PuPpIuE" 31 | } 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "source": [ 36 | "在这里写下您的prompt提示。 描述性越强、越清晰越好!\n", 37 | "然后,选择生成数据时使用的temperature(0 到 1 之间)。 较低的值非常适合精确的任务,例如编写代码,而较大的值更适合创造性的任务,例如编写故事。\n", 38 | "最后,选择您想要生成的示例数量。 生成的数据越多,a) 花费的时间就越长,b) 数据生成的成本就越高。 但一般来说,更多的例子会产生更高质量的模型。 100 通常是最低启动值。" 39 | ], 40 | "metadata": { 41 | "id": "lY-3DvlIpVSl" 42 | } 43 | }, 44 | { 45 | "cell_type": "code", 46 | "source": [ 47 | "prompt = \"一个能够接收中文中类似猜灯谜的问题,并用经过深思熟虑、逐步推理的方式以中文回答的模型。\"\n", 48 | "temperature = .4\n", 49 | "number_of_examples = 100" 50 | ], 51 | "metadata": { 52 | "id": "R7WKZyxtpUPS" 53 | }, 54 | "execution_count": null, 55 | "outputs": [] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "source": [ 60 | "\n", 61 | "先安装OpenAI依赖。" 62 | ], 63 | "metadata": { 64 | "id": "1snNou5PrIci" 65 | } 66 | }, 67 | { 68 | "cell_type": "code", 69 | "source": [ 70 | "!pip install openai" 71 | ], 72 | "metadata": { 73 | "id": "zuL2UaqlsmBD" 74 | }, 75 | "execution_count": null, 76 | "outputs": [] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "source": [ 81 | "一旦你确定了提示,就可以执行下面的代码生成数据集。这可能需要比预期更长的时间。" 82 | ], 83 | "metadata": { 84 | "id": "0GQNqqOZqbST" 85 | } 86 | }, 87 | { 88 | "cell_type": "code", 89 | "source": [ 90 | "import os\n", 91 | "import openai\n", 92 | "import random\n", 93 | "\n", 94 | "openai.api_key = \"YOUR KEY HERE\"\n", 95 | "\n", 96 | "def generate_example(prompt, prev_examples, temperature=.5):\n", 97 | " messages=[\n", 98 | " {\n", 99 | " \"role\": \"system\",\n", 100 | " \"content\": f\"您正在生成将用于训练机器学习模型的数据。\\n\\n您将获得我们想要训练的模型的高级描述,并由此生成数据样本,每个样本都有一个提示/ 响应对。\\n\\n您将按照以下格式执行此操作:\\n````\\nprompt\\n------------\\n$prompt_goes_here\\n---------- -\\n\\nresponse\\n-----------\\n$response_goes_here\\n------------\\n```\\n\\n只能有一对提示/响应 每轮都会生成。\\n\\n对于每一轮,使示例比上一轮稍微复杂一点,同时确保多样性。\\n\\n确保您的样本是独特且多样化的,但高质量和复杂性足以训练一个良好的样本 执行模型。\\n\\n这是我们要训练的模型类型:\\n`{prompt}`\"\n", 101 | " }\n", 102 | " ]\n", 103 | "\n", 104 | " if len(prev_examples) > 0:\n", 105 | " if len(prev_examples) > 10:\n", 106 | " prev_examples = random.sample(prev_examples, 10)\n", 107 | " for example in prev_examples:\n", 108 | " messages.append({\n", 109 | " \"role\": \"assistant\",\n", 110 | " \"content\": example\n", 111 | " })\n", 112 | "\n", 113 | " response = openai.ChatCompletion.create(\n", 114 | " model=\"gpt-4\",\n", 115 | " messages=messages,\n", 116 | " temperature=temperature,\n", 117 | " max_tokens=1354,\n", 118 | " )\n", 119 | "\n", 120 | " return response.choices[0].message['content']\n", 121 | "\n", 122 | "# Generate examples\n", 123 | "prev_examples = []\n", 124 | "for i in range(number_of_examples):\n", 125 | " print(f'Generating example {i}')\n", 126 | " example = generate_example(prompt, prev_examples, temperature)\n", 127 | " prev_examples.append(example)\n", 128 | "\n", 129 | "print(prev_examples)" 130 | ], 131 | "metadata": { 132 | "id": "Rdsd82ngpHCG" 133 | }, 134 | "execution_count": null, 135 | "outputs": [] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "source": [ 140 | "生成完数据集,我们还需要生成系统消息。" 141 | ], 142 | "metadata": { 143 | "id": "KC6iJzXjugJ-" 144 | } 145 | }, 146 | { 147 | "cell_type": "code", 148 | "source": [ 149 | "def generate_system_message(prompt):\n", 150 | "\n", 151 | " response = openai.ChatCompletion.create(\n", 152 | " model=\"gpt-4\",\n", 153 | " messages=[\n", 154 | " {\n", 155 | " \"role\": \"system\",\n", 156 | " \"content\": \"您将获得我们正在训练的模型的高级描述,并据此生成一个简单的系统提示以供该模型使用。 请记住,您不是生成用于数据生成的系统消息 - 您正在生成用于推理的系统消息。 一个好的格式是“给定 $INPUT_DATA,您将 $WHAT_THE_MODEL_SHOULD_DO。”。\\n\\n使其尽可能简洁。 在响应中只包含系统提示符。\\n\\n例如,切勿编写:`\\\"$SYSTEM_PROMPT_HERE\\\"`。\\n\\n应该类似于:`$SYSTEM_PROMPT_HERE`。\"\n", 157 | " },\n", 158 | " {\n", 159 | " \"role\": \"user\",\n", 160 | " \"content\": prompt.strip(),\n", 161 | " }\n", 162 | " ],\n", 163 | " temperature=temperature,\n", 164 | " max_tokens=500,\n", 165 | " )\n", 166 | "\n", 167 | " return response.choices[0].message['content']\n", 168 | "\n", 169 | "system_message = generate_system_message(prompt)\n", 170 | "\n", 171 | "print(f'系统消息是: `{system_message}`。 如果您想要更好的结果,请随意重新运行此单元格。')" 172 | ], 173 | "metadata": { 174 | "id": "xMcfhW6Guh2E" 175 | }, 176 | "execution_count": null, 177 | "outputs": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "source": [ 182 | "接下来,我们将示例放入数据框中,并将它们转换为最终的数据集对。" 183 | ], 184 | "metadata": { 185 | "id": "G6BqZ-hjseBF" 186 | } 187 | }, 188 | { 189 | "cell_type": "code", 190 | "source": [ 191 | "import pandas as pd\n", 192 | "\n", 193 | "# 初始化列表以存储提示和响应\n", 194 | "prompts = []\n", 195 | "responses = []\n", 196 | "\n", 197 | "# 从示例中解析出提示和响应\n", 198 | "for example in prev_examples:\n", 199 | " try:\n", 200 | " split_example = example.split('-----------')\n", 201 | " prompts.append(split_example[1].strip())\n", 202 | " responses.append(split_example[3].strip())\n", 203 | " except:\n", 204 | " pass\n", 205 | "\n", 206 | "# 创建数据框\n", 207 | "df = pd.DataFrame({\n", 208 | " 'prompt': prompts,\n", 209 | " 'response': responses\n", 210 | "})\n", 211 | "\n", 212 | "# 删除重复项\n", 213 | "df = df.drop_duplicates()\n", 214 | "\n", 215 | "print('有 ' + str(len(df)) + ' 成功生成的示例。 以下是前几个:')\n", 216 | "\n", 217 | "df.head()" 218 | ], 219 | "metadata": { 220 | "id": "7CEdkYeRsdmB" 221 | }, 222 | "execution_count": null, 223 | "outputs": [] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "source": [ 228 | "将数据分为训练集和测试集。" 229 | ], 230 | "metadata": { 231 | "id": "A-8dt5qqtpgM" 232 | } 233 | }, 234 | { 235 | "cell_type": "code", 236 | "source": [ 237 | "# 将数据分为训练集和测试集,其中 90% 在训练集中\n", 238 | "train_df = df.sample(frac=0.9, random_state=42)\n", 239 | "test_df = df.drop(train_df.index)\n", 240 | "\n", 241 | "# 将数据帧保存到 .jsonl 文件\n", 242 | "train_df.to_json('train.jsonl', orient='records', lines=True)\n", 243 | "test_df.to_json('test.jsonl', orient='records', lines=True)" 244 | ], 245 | "metadata": { 246 | "id": "GFPEn1omtrXM" 247 | }, 248 | "execution_count": null, 249 | "outputs": [] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "source": [ 254 | "# 安装必要的库" 255 | ], 256 | "metadata": { 257 | "id": "AbrFgrhG_xYi" 258 | } 259 | }, 260 | { 261 | "cell_type": "code", 262 | "source": [ 263 | "!pip install -q accelerate==0.21.0 peft==0.4.0 bitsandbytes==0.40.2 transformers==4.31.0 trl==0.4.7\n", 264 | "\n", 265 | "import os\n", 266 | "import torch\n", 267 | "from datasets import load_dataset\n", 268 | "from transformers import (\n", 269 | " AutoModelForCausalLM,\n", 270 | " AutoTokenizer,\n", 271 | " BitsAndBytesConfig,\n", 272 | " HfArgumentParser,\n", 273 | " TrainingArguments,\n", 274 | " pipeline,\n", 275 | " logging,\n", 276 | ")\n", 277 | "from peft import LoraConfig, PeftModel\n", 278 | "from trl import SFTTrainer" 279 | ], 280 | "metadata": { 281 | "id": "lPG7wEPetFx2" 282 | }, 283 | "execution_count": null, 284 | "outputs": [] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "source": [ 289 | "# 定义超参数" 290 | ], 291 | "metadata": { 292 | "id": "moVo0led-6tu" 293 | } 294 | }, 295 | { 296 | "cell_type": "code", 297 | "source": [ 298 | "model_name = \"NousResearch/llama-2-7b-chat-hf\" # 如果您有权访问官方 LLaMA 2 模型“meta-llama/Llama-2-7b-chat-hf”,请使用此选项,但请记住,您需要传递 Hugging Face 键参数\n", 299 | "dataset_name = \"/content/train.jsonl\"\n", 300 | "new_model = \"llama-2-7b-custom\"\n", 301 | "lora_r = 64\n", 302 | "lora_alpha = 16\n", 303 | "lora_dropout = 0.1\n", 304 | "use_4bit = True\n", 305 | "bnb_4bit_compute_dtype = \"float16\"\n", 306 | "bnb_4bit_quant_type = \"nf4\"\n", 307 | "use_nested_quant = False\n", 308 | "output_dir = \"./results\"\n", 309 | "num_train_epochs = 1\n", 310 | "fp16 = False\n", 311 | "bf16 = False\n", 312 | "per_device_train_batch_size = 4\n", 313 | "per_device_eval_batch_size = 4\n", 314 | "gradient_accumulation_steps = 1\n", 315 | "gradient_checkpointing = True\n", 316 | "max_grad_norm = 0.3\n", 317 | "learning_rate = 2e-4\n", 318 | "weight_decay = 0.001\n", 319 | "optim = \"paged_adamw_32bit\"\n", 320 | "lr_scheduler_type = \"constant\"\n", 321 | "max_steps = -1\n", 322 | "warmup_ratio = 0.03\n", 323 | "group_by_length = True\n", 324 | "save_steps = 25\n", 325 | "logging_steps = 5\n", 326 | "max_seq_length = None\n", 327 | "packing = False\n", 328 | "device_map = {\"\": 0}" 329 | ], 330 | "metadata": { 331 | "id": "bqfbhUZI-4c_" 332 | }, 333 | "execution_count": null, 334 | "outputs": [] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "source": [ 339 | "#加载数据集并训练\n" 340 | ], 341 | "metadata": { 342 | "id": "F-J5p5KS_MZY" 343 | } 344 | }, 345 | { 346 | "cell_type": "code", 347 | "source": [ 348 | "# 加载数据集\n", 349 | "train_dataset = load_dataset('json', data_files='/content/train.jsonl', split=\"train\")\n", 350 | "valid_dataset = load_dataset('json', data_files='/content/test.jsonl', split=\"train\")\n", 351 | "\n", 352 | "# 预处理数据集\n", 353 | "train_dataset_mapped = train_dataset.map(lambda examples: {'text': [f'[INST] <>\\n{system_message.strip()}\\n<>\\n\\n' + prompt + ' [/INST] ' + response for prompt, response in zip(examples['prompt'], examples['response'])]}, batched=True)\n", 354 | "valid_dataset_mapped = valid_dataset.map(lambda examples: {'text': [f'[INST] <>\\n{system_message.strip()}\\n<>\\n\\n' + prompt + ' [/INST] ' + response for prompt, response in zip(examples['prompt'], examples['response'])]}, batched=True)\n", 355 | "\n", 356 | "compute_dtype = getattr(torch, bnb_4bit_compute_dtype)\n", 357 | "bnb_config = BitsAndBytesConfig(\n", 358 | " load_in_4bit=use_4bit,\n", 359 | " bnb_4bit_quant_type=bnb_4bit_quant_type,\n", 360 | " bnb_4bit_compute_dtype=compute_dtype,\n", 361 | " bnb_4bit_use_double_quant=use_nested_quant,\n", 362 | ")\n", 363 | "model = AutoModelForCausalLM.from_pretrained(\n", 364 | " model_name,\n", 365 | " quantization_config=bnb_config,\n", 366 | " device_map=device_map\n", 367 | ")\n", 368 | "model.config.use_cache = False\n", 369 | "model.config.pretraining_tp = 1\n", 370 | "tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\n", 371 | "tokenizer.pad_token = tokenizer.eos_token\n", 372 | "tokenizer.padding_side = \"right\"\n", 373 | "peft_config = LoraConfig(\n", 374 | " lora_alpha=lora_alpha,\n", 375 | " lora_dropout=lora_dropout,\n", 376 | " r=lora_r,\n", 377 | " bias=\"none\",\n", 378 | " task_type=\"CAUSAL_LM\",\n", 379 | ")\n", 380 | "# 设置训练参数\n", 381 | "training_arguments = TrainingArguments(\n", 382 | " output_dir=output_dir,\n", 383 | " num_train_epochs=num_train_epochs,\n", 384 | " per_device_train_batch_size=per_device_train_batch_size,\n", 385 | " gradient_accumulation_steps=gradient_accumulation_steps,\n", 386 | " optim=optim,\n", 387 | " save_steps=save_steps,\n", 388 | " logging_steps=logging_steps,\n", 389 | " learning_rate=learning_rate,\n", 390 | " weight_decay=weight_decay,\n", 391 | " fp16=fp16,\n", 392 | " bf16=bf16,\n", 393 | " max_grad_norm=max_grad_norm,\n", 394 | " max_steps=max_steps,\n", 395 | " warmup_ratio=warmup_ratio,\n", 396 | " group_by_length=group_by_length,\n", 397 | " lr_scheduler_type=lr_scheduler_type,\n", 398 | " report_to=\"all\",\n", 399 | " evaluation_strategy=\"steps\",\n", 400 | " eval_steps=5 # 每 20 步评估一次\n", 401 | ")\n", 402 | "# 设置监督微调参数\n", 403 | "trainer = SFTTrainer(\n", 404 | " model=model,\n", 405 | " train_dataset=train_dataset_mapped,\n", 406 | " eval_dataset=valid_dataset_mapped, # 在此处传递验证数据集\n", 407 | " peft_config=peft_config,\n", 408 | " dataset_text_field=\"text\",\n", 409 | " max_seq_length=max_seq_length,\n", 410 | " tokenizer=tokenizer,\n", 411 | " args=training_arguments,\n", 412 | " packing=packing,\n", 413 | ")\n", 414 | "trainer.train()\n", 415 | "trainer.model.save_pretrained(new_model)\n", 416 | "\n", 417 | "# 单元 4:测试模型\n", 418 | "logging.set_verbosity(logging.CRITICAL)\n", 419 | "prompt = f\"[INST] <>\\n{system_message}\\n<>\\n\\n编写一个反转字符串的函数。 [/INST]\" # 将此处的命令替换为与您的任务相关的命令\n", 420 | "pipe = pipeline(task=\"text-generation\", model=model, tokenizer=tokenizer, max_length=200)\n", 421 | "result = pipe(prompt)\n", 422 | "print(result[0]['generated_text'])" 423 | ], 424 | "metadata": { 425 | "id": "qf1qxbiF-x6p" 426 | }, 427 | "execution_count": null, 428 | "outputs": [] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "source": [ 433 | "#运行推理" 434 | ], 435 | "metadata": { 436 | "id": "F6fux9om_c4-" 437 | } 438 | }, 439 | { 440 | "cell_type": "code", 441 | "source": [ 442 | "from transformers import pipeline\n", 443 | "\n", 444 | "prompt = f\"[INST] <>\\n{system_message}\\n<>\\n\\n编写一个反转字符串的函数。 [/INST]\" # 将此处的命令替换为与您的任务相关的命令\n", 445 | "num_new_tokens = 100 # 更改为您想要生成的新令牌的数量\n", 446 | "\n", 447 | "# 计算提示中的标记数量\n", 448 | "num_prompt_tokens = len(tokenizer(prompt)['input_ids'])\n", 449 | "\n", 450 | "# 计算一代的最大长度\n", 451 | "max_length = num_prompt_tokens + num_new_tokens\n", 452 | "\n", 453 | "gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length)\n", 454 | "result = gen(prompt)\n", 455 | "print(result[0]['generated_text'].replace(prompt, ''))" 456 | ], 457 | "metadata": { 458 | "id": "7hxQ_Ero2IJe" 459 | }, 460 | "execution_count": null, 461 | "outputs": [] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "source": [ 466 | "#合并模型并存储在 Google Drive 中" 467 | ], 468 | "metadata": { 469 | "id": "Ko6UkINu_qSx" 470 | } 471 | }, 472 | { 473 | "cell_type": "code", 474 | "source": [ 475 | "# 合并并保存微调后的模型\n", 476 | "from google.colab import drive\n", 477 | "drive.mount('/content/drive')\n", 478 | "\n", 479 | "model_path = \"/content/drive/MyDrive/llama-2-7b-custom\" # 更改为您的首选路径\n", 480 | "\n", 481 | "# 在 FP16 中重新加载模型并将其与 LoRA 权重合并\n", 482 | "base_model = AutoModelForCausalLM.from_pretrained(\n", 483 | " model_name,\n", 484 | " low_cpu_mem_usage=True,\n", 485 | " return_dict=True,\n", 486 | " torch_dtype=torch.float16,\n", 487 | " device_map=device_map,\n", 488 | ")\n", 489 | "model = PeftModel.from_pretrained(base_model, new_model)\n", 490 | "model = model.merge_and_unload()\n", 491 | "\n", 492 | "# 重新加载分词器以保存它\n", 493 | "tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\n", 494 | "tokenizer.pad_token = tokenizer.eos_token\n", 495 | "tokenizer.padding_side = \"right\"\n", 496 | "\n", 497 | "# 保存合并后的模型\n", 498 | "model.save_pretrained(model_path)\n", 499 | "tokenizer.save_pretrained(model_path)" 500 | ], 501 | "metadata": { 502 | "id": "AgKCL7fTyp9u" 503 | }, 504 | "execution_count": null, 505 | "outputs": [] 506 | }, 507 | { 508 | "cell_type": "markdown", 509 | "source": [ 510 | "# 从 Drive 加载微调模型并运行推理" 511 | ], 512 | "metadata": { 513 | "id": "do-dFdE5zWGO" 514 | } 515 | }, 516 | { 517 | "cell_type": "code", 518 | "source": [ 519 | "from google.colab import drive\n", 520 | "from transformers import AutoModelForCausalLM, AutoTokenizer\n", 521 | "\n", 522 | "drive.mount('/content/drive')\n", 523 | "\n", 524 | "model_path = \"/content/drive/MyDrive/llama-2-7b-custom\" # 更改为保存模型的路径\n", 525 | "\n", 526 | "model = AutoModelForCausalLM.from_pretrained(model_path)\n", 527 | "tokenizer = AutoTokenizer.from_pretrained(model_path)" 528 | ], 529 | "metadata": { 530 | "id": "xg6nHPsLzMw-" 531 | }, 532 | "execution_count": null, 533 | "outputs": [] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "source": [ 538 | "from transformers import pipeline\n", 539 | "\n", 540 | "prompt = \"请问,哪个字一年四季都不会凋零?\" # 更改为您想要的提示\n", 541 | "gen = pipeline('text-generation', model=model, tokenizer=tokenizer)\n", 542 | "result = gen(prompt)\n", 543 | "print(result[0]['generated_text'])" 544 | ], 545 | "metadata": { 546 | "id": "fBK2aE2KzZ05" 547 | }, 548 | "execution_count": null, 549 | "outputs": [] 550 | }, 551 | { 552 | "cell_type": "markdown", 553 | "source": [ 554 | "##总结\n", 555 | "GPT-LLM-Trainer 是一款经济实惠且易于使用的工具,用于使用您自己的数据训练大型语言模型。它简化了收集、提炼、格式化、选择和训练数据集的复杂过程,并根据您的任务描述为您微调模型。使用此工具,您可以生成各种提示、优化系统提示、拆分数据集、定义超参数以及在 Google Colab 或本地 Jupyter Notebook 上高效运行推理。" 556 | ], 557 | "metadata": { 558 | "id": "imITgpl4qq16" 559 | } 560 | } 561 | ], 562 | "metadata": { 563 | "accelerator": "GPU", 564 | "colab": { 565 | "machine_shape": "hm", 566 | "provenance": [], 567 | "gpuType": "T4", 568 | "include_colab_link": true 569 | }, 570 | "kernelspec": { 571 | "display_name": "Python 3", 572 | "name": "python3" 573 | }, 574 | "language_info": { 575 | "name": "python" 576 | } 577 | }, 578 | "nbformat": 4, 579 | "nbformat_minor": 0 580 | } 581 | -------------------------------------------------------------------------------- /fine-tune-code-llama/fine_tune_code_llama.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "_5rfduRD1CSF" 17 | }, 18 | "source": [ 19 | "# Code Llama 微调指南\n", 20 | "\n", 21 | "**在本指南中,我将向您展示如何微调 Code Llama,使其成为 SQL 开发人员的野兽。对于编码任务,您通常可以从 Code Llama 获得比 Llama 2 更好的性能,特别是当您将模型专门用于特定任务时:**\n", 22 | "\n", 23 | "- 使用 [b-mc2/sql-create-context](https://huggingface.co/datasets/b-mc2/sql-create-context) 这是一堆文本查询及其相应的 SQL 查询\n", 24 | "- Lora 方法,将基本模型量化为 int 8,冻结其权重,仅训练适配器\n", 25 | "- 大部分代码是从 [alpaca-lora](https://github.com/tloen/alpaca-lora) 参考的,同时也进行了一定的改进与优化\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": { 31 | "id": "HFQ9XeBI1CSG" 32 | }, 33 | "source": [ 34 | "### 2. Pip installs\n" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": { 41 | "id": "Y0x4OWfq1CSG" 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "!pip install git+https://github.com/huggingface/transformers.git@main bitsandbytes accelerate==0.20.3 # we need latest transformers for this\n", 46 | "!pip install git+https://github.com/huggingface/peft.git@e536616888d51b453ed354a6f1e243fecb02ea08\n", 47 | "!pip install datasets==2.10.1\n", 48 | "import locale # colab workaround\n", 49 | "locale.getpreferredencoding = lambda: \"UTF-8\" # colab workaround\n", 50 | "!pip install wandb" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": { 56 | "id": "mCmGzYg51CSH" 57 | }, 58 | "source": [ 59 | "我使用了一台配置了 Python 3.10 和 Cuda 11.8 的 A100 GPU 服务器来运行本文中的代码。大约运行了一个小时。(为了验证可移植性,我还试验在Colab上运行代码,效果都很好。)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": { 65 | "id": "0mu9JczX1CSH" 66 | }, 67 | "source": [ 68 | "### 加载库\n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": { 75 | "id": "oTeYW8z51CSH" 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "from datetime import datetime\n", 80 | "import os\n", 81 | "import sys\n", 82 | "\n", 83 | "import torch\n", 84 | "from peft import (\n", 85 | " LoraConfig,\n", 86 | " get_peft_model,\n", 87 | " get_peft_model_state_dict,\n", 88 | " prepare_model_for_int8_training,\n", 89 | " set_peft_model_state_dict,\n", 90 | ")\n", 91 | "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForSeq2Seq\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "id": "32zH9-hM1CSH" 98 | }, 99 | "source": [ 100 | "(如果出现导入​​错误,请尝试重新启动 Jupyter 内核)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": { 106 | "id": "4M9KyT0S1CSH" 107 | }, 108 | "source": [ 109 | "### 加载数据集\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": { 116 | "id": "w44O1EK-1CSH" 117 | }, 118 | "outputs": [], 119 | "source": [ 120 | "from datasets import load_dataset\n", 121 | "dataset = load_dataset(\"b-mc2/sql-create-context\", split=\"train\")\n", 122 | "train_dataset = dataset.train_test_split(test_size=0.1)[\"train\"]\n", 123 | "eval_dataset = dataset.train_test_split(test_size=0.1)[\"test\"]" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": { 129 | "id": "fJ54EffO1CSI" 130 | }, 131 | "source": [ 132 | "上面从 Huggingface Hub 中提取数据集,并将其中的 10% 分成评估集,以检查模型在训练中的表现如何。如果您想加载自己的数据集,请执行以下操作:\n", 133 | "\n", 134 | "```\n", 135 | "train_dataset = load_dataset('json', data_files='train_set.jsonl', split='train')\n", 136 | "eval_dataset = load_dataset('json', data_files='validation_set.jsonl', split='train')\n", 137 | "```\n", 138 | "\n", 139 | "如果您想查看数据集中的任何样本,只需执行以下操作:``` ```\n" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": { 146 | "id": "fFbeaZzf1CSJ" 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "print(train_dataset[3])" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": { 156 | "id": "VHdMYcu61CSJ" 157 | }, 158 | "source": [ 159 | "每个条目由文本“问题”、sql 表“上下文”和“答案”组成。" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": { 165 | "id": "6ig7NvWN1CSJ" 166 | }, 167 | "source": [ 168 | "### 加载模型\n", 169 | "我从 Huggingface 中以 int8 加载代码 llama。Lora的标准:" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": { 176 | "id": "rMnU93bY1CSJ" 177 | }, 178 | "outputs": [], 179 | "source": [ 180 | "base_model = \"codellama/CodeLlama-7b-hf\"\n", 181 | "model = AutoModelForCausalLM.from_pretrained(\n", 182 | " base_model,\n", 183 | " load_in_8bit=True,\n", 184 | " torch_dtype=torch.float16,\n", 185 | " device_map=\"auto\",\n", 186 | ")\n", 187 | "tokenizer = AutoTokenizer.from_pretrained(\"codellama/CodeLlama-7b-hf\")" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": { 193 | "id": "F3OF-wtj1CSJ" 194 | }, 195 | "source": [ 196 | "torch_dtype=torch.float16 表示使用 float16 表示形式执行计算,即使值本身是 8 位整数。\n", 197 | "\n", 198 | "如果出现错误“ValueError:Tokenizer 类 CodeLlamaTokenizer 不存在或当前未导入。”确保您的 Transformer 版本为 4.33.0.dev0 并且加速 >=0.20.3。\n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": { 204 | "id": "_2VXqJJe1CSJ" 205 | }, 206 | "source": [ 207 | "### 3. 检查基础型号\n", 208 | "一个非常好的常见做法是检查模型是否已经可以完成手头的任务。微调是您要不惜一切代价尽量避免的事情:\n" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "id": "uiyAff1a1CSJ" 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "eval_prompt = \"\"\"You are a powerful text-to-SQL model. Your job is to answer questions about a database. You are given a question and context regarding one or more tables.\n", 220 | "\n", 221 | "You must output the SQL query that answers the question.\n", 222 | "### Input:\n", 223 | "Which Class has a Frequency MHz larger than 91.5, and a City of license of hyannis, nebraska?\n", 224 | "\n", 225 | "### Context:\n", 226 | "CREATE TABLE table_name_12 (class VARCHAR, frequency_mhz VARCHAR, city_of_license VARCHAR)\n", 227 | "\n", 228 | "### Response:\n", 229 | "\"\"\"\n", 230 | "# {'question': 'Name the comptroller for office of prohibition', 'context': 'CREATE TABLE table_22607062_1 (comptroller VARCHAR, ticket___office VARCHAR)', 'answer': 'SELECT comptroller FROM table_22607062_1 WHERE ticket___office = \"Prohibition\"'}\n", 231 | "model_input = tokenizer(eval_prompt, return_tensors=\"pt\").to(\"cuda\")\n", 232 | "\n", 233 | "model.eval()\n", 234 | "with torch.no_grad():\n", 235 | " print(tokenizer.decode(model.generate(**model_input, max_new_tokens=100)[0], skip_special_tokens=True))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": { 241 | "id": "544rmsdU1CSJ" 242 | }, 243 | "source": [ 244 | "输出结果:\n", 245 | "```\n", 246 | "SELECT * FROM table_name_12 WHERE class > 91.5 AND city_of_license = 'hyannis, nebraska'\n", 247 | "```\n", 248 | "如果输入只要求上课,这显然是错误的!" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": { 254 | "id": "puuOXL2R1CSJ" 255 | }, 256 | "source": [ 257 | "### 4. Tokenization\n", 258 | "设置一些标记化设置,例如左填充,因为它使[训练使用更少的内存](https://ai.stackexchange.com/questions/41485/while-fine-tuning-a-decoder-only-llm-like-llama-on-chat-dataset-what-kind-of-pa):" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "id": "c1P0mphA1CSJ" 266 | }, 267 | "outputs": [], 268 | "source": [ 269 | "tokenizer.add_eos_token = True\n", 270 | "tokenizer.pad_token_id = 0\n", 271 | "tokenizer.padding_side = \"left\"" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": { 277 | "id": "kpo66hMo1CSJ" 278 | }, 279 | "source": [ 280 | "设置 tokenize 函数以使 labels 和 input_ids 相同。这基本上就是[自我监督微调](https://neptune.ai/blog/self-supervised-learning):" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": { 287 | "id": "gjSerml71CSJ" 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "def tokenize(prompt):\n", 292 | " result = tokenizer(\n", 293 | " prompt,\n", 294 | " truncation=True,\n", 295 | " max_length=512,\n", 296 | " padding=False,\n", 297 | " return_tensors=None,\n", 298 | " )\n", 299 | "\n", 300 | " # \"self-supervised learning\" means the labels are also the inputs:\n", 301 | " result[\"labels\"] = result[\"input_ids\"].copy()\n", 302 | "\n", 303 | " return result" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": { 309 | "id": "fNk_3THv1CSK" 310 | }, 311 | "source": [ 312 | "并运行将每个 data_point 转换为我在网上找到的效果很好的提示:" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": { 319 | "id": "MvO7A-ZF1CSK" 320 | }, 321 | "outputs": [], 322 | "source": [ 323 | "def generate_and_tokenize_prompt(data_point):\n", 324 | " full_prompt =f\"\"\"You are a powerful text-to-SQL model. Your job is to answer questions about a database. You are given a question and context regarding one or more tables.\n", 325 | "\n", 326 | "You must output the SQL query that answers the question.\n", 327 | "\n", 328 | "### Input:\n", 329 | "{data_point[\"question\"]}\n", 330 | "\n", 331 | "### Context:\n", 332 | "{data_point[\"context\"]}\n", 333 | "\n", 334 | "### Response:\n", 335 | "{data_point[\"answer\"]}\n", 336 | "\"\"\"\n", 337 | " return tokenize(full_prompt)" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": { 343 | "id": "sLvwo-ax1CSK" 344 | }, 345 | "source": [ 346 | "重新格式化以提示并标记每个样本:" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": { 353 | "id": "SA2BqSzW1CSK" 354 | }, 355 | "outputs": [], 356 | "source": [ 357 | "tokenized_train_dataset = train_dataset.map(generate_and_tokenize_prompt)\n", 358 | "tokenized_val_dataset = eval_dataset.map(generate_and_tokenize_prompt)" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": { 364 | "id": "Z5ByhitV1CSK" 365 | }, 366 | "source": [ 367 | "### 5. 设置 Lora" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": { 374 | "id": "9q_26pz71CSK" 375 | }, 376 | "outputs": [], 377 | "source": [ 378 | "model.train() # put model back into training mode\n", 379 | "model = prepare_model_for_int8_training(model)\n", 380 | "\n", 381 | "config = LoraConfig(\n", 382 | " r=16,\n", 383 | " lora_alpha=16,\n", 384 | " target_modules=[\n", 385 | " \"q_proj\",\n", 386 | " \"k_proj\",\n", 387 | " \"v_proj\",\n", 388 | " \"o_proj\",\n", 389 | "],\n", 390 | " lora_dropout=0.05,\n", 391 | " bias=\"none\",\n", 392 | " task_type=\"CAUSAL_LM\",\n", 393 | ")\n", 394 | "model = get_peft_model(model, config)" 395 | ] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "metadata": { 400 | "id": "IvkQ3igC1CSK" 401 | }, 402 | "source": [ 403 | "要从检查点恢复,请将resume_from_checkpoint 设置为要从中恢复的adapter_model.bin 的路径。此代码将替换连接到模型的 lora 适配器:" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": null, 409 | "metadata": { 410 | "id": "TMTxR2JT1CSK" 411 | }, 412 | "outputs": [], 413 | "source": [ 414 | "resume_from_checkpoint = \"\" # set this to the adapter_model.bin file you want to resume from\n", 415 | "\n", 416 | "if resume_from_checkpoint:\n", 417 | " if os.path.exists(resume_from_checkpoint):\n", 418 | " print(f\"Restarting from {resume_from_checkpoint}\")\n", 419 | " adapters_weights = torch.load(resume_from_checkpoint)\n", 420 | " set_peft_model_state_dict(model, adapters_weights)\n", 421 | " else:\n", 422 | " print(f\"Checkpoint {resume_from_checkpoint} not found\")" 423 | ] 424 | }, 425 | { 426 | "cell_type": "markdown", 427 | "metadata": { 428 | "id": "GW1wtvbP1CSK" 429 | }, 430 | "source": [ 431 | "设置权重和偏差以查看训练图的可选内容:" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "metadata": { 438 | "id": "AC1_qWbS1CSK" 439 | }, 440 | "outputs": [], 441 | "source": [ 442 | "wandb_project = \"sql-try2-coder\"\n", 443 | "if len(wandb_project) > 0:\n", 444 | " os.environ[\"WANDB_PROJECT\"] = wandb_project\n" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": null, 450 | "metadata": { 451 | "id": "5iv4txxu1CSK" 452 | }, 453 | "outputs": [], 454 | "source": [ 455 | "if torch.cuda.device_count() > 1:\n", 456 | " # keeps Trainer from trying its own DataParallelism when more than 1 gpu is available\n", 457 | " model.is_parallelizable = True\n", 458 | " model.model_parallel = True" 459 | ] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": { 464 | "id": "pYSsnciQ1CSK" 465 | }, 466 | "source": [ 467 | "### 6. 训练参数\n", 468 | "如果 GPU 内存不足,请更改 per_device_train_batch_size。 gradient_accumulation_steps 变量应确保这不会影响训练运行期间的批量动态。所有其他变量都是标准的东西,我不建议乱搞:" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": null, 474 | "metadata": { 475 | "id": "uvEi1kP21CSK" 476 | }, 477 | "outputs": [], 478 | "source": [ 479 | "batch_size = 128\n", 480 | "per_device_train_batch_size = 32\n", 481 | "gradient_accumulation_steps = batch_size // per_device_train_batch_size\n", 482 | "output_dir = \"sql-code-llama\"\n", 483 | "\n", 484 | "training_args = TrainingArguments(\n", 485 | " per_device_train_batch_size=per_device_train_batch_size,\n", 486 | " gradient_accumulation_steps=gradient_accumulation_steps,\n", 487 | " warmup_steps=100,\n", 488 | " max_steps=400,\n", 489 | " learning_rate=3e-4,\n", 490 | " fp16=True,\n", 491 | " logging_steps=10,\n", 492 | " optim=\"adamw_torch\",\n", 493 | " evaluation_strategy=\"steps\", # if val_set_size > 0 else \"no\",\n", 494 | " save_strategy=\"steps\",\n", 495 | " eval_steps=20,\n", 496 | " save_steps=20,\n", 497 | " output_dir=output_dir,\n", 498 | " # save_total_limit=3,\n", 499 | " load_best_model_at_end=False,\n", 500 | " # ddp_find_unused_parameters=False if ddp else None,\n", 501 | " group_by_length=True, # group sequences of roughly the same length together to speed up training\n", 502 | " report_to=\"wandb\", # if use_wandb else \"none\",\n", 503 | " run_name=f\"codellama-{datetime.now().strftime('%Y-%m-%d-%H-%M')}\", # if use_wandb else None,\n", 504 | " )\n", 505 | "\n", 506 | "trainer = Trainer(\n", 507 | " model=model,\n", 508 | " train_dataset=tokenized_train_dataset,\n", 509 | " eval_dataset=tokenized_val_dataset,\n", 510 | " args=training_args,\n", 511 | " data_collator=DataCollatorForSeq2Seq(\n", 512 | " tokenizer, pad_to_multiple_of=8, return_tensors=\"pt\", padding=True\n", 513 | " ),\n", 514 | ")" 515 | ] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "metadata": { 520 | "id": "1aJp6Jxl1CSK" 521 | }, 522 | "source": [ 523 | "然后我们进行一些与 pytorch 相关的优化,这只是使训练更快,但不影响准确性:" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": null, 529 | "metadata": { 530 | "id": "6ycCHZZl1CSK" 531 | }, 532 | "outputs": [], 533 | "source": [ 534 | "model.config.use_cache = False\n", 535 | "\n", 536 | "old_state_dict = model.state_dict\n", 537 | "model.state_dict = (lambda self, *_, **__: get_peft_model_state_dict(self, old_state_dict())).__get__(\n", 538 | " model, type(model)\n", 539 | ")\n", 540 | "if torch.__version__ >= \"2\" and sys.platform != \"win32\":\n", 541 | " print(\"compiling the model\")\n", 542 | " model = torch.compile(model)" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": null, 548 | "metadata": { 549 | "id": "bF5oWKxK1CSL" 550 | }, 551 | "outputs": [], 552 | "source": [ 553 | "trainer.train()" 554 | ] 555 | }, 556 | { 557 | "cell_type": "markdown", 558 | "metadata": { 559 | "id": "j1dRQLMT1CSU" 560 | }, 561 | "source": [ 562 | "### 加载最终检查点\n" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": { 569 | "id": "sRdVgDTg1CSU" 570 | }, 571 | "outputs": [], 572 | "source": [ 573 | "import torch\n", 574 | "from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer\n", 575 | "\n", 576 | "base_model = \"codellama/CodeLlama-7b-hf\"\n", 577 | "model = AutoModelForCausalLM.from_pretrained(\n", 578 | " base_model,\n", 579 | " load_in_8bit=True,\n", 580 | " torch_dtype=torch.float16,\n", 581 | " device_map=\"auto\",\n", 582 | ")\n", 583 | "tokenizer = AutoTokenizer.from_pretrained(\"codellama/CodeLlama-7b-hf\")" 584 | ] 585 | }, 586 | { 587 | "cell_type": "markdown", 588 | "metadata": { 589 | "id": "76UlHzhy1CSU" 590 | }, 591 | "source": [ 592 | "要加载经过微调的 Lora/Qlora 适配器,请使用 PeftModel.from_pretrained。 `output_dir` 应该是包含adapter_config.json和adapter_model.bin的东西:" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "metadata": { 599 | "id": "rQrCR0os1CSU" 600 | }, 601 | "outputs": [], 602 | "source": [ 603 | "from peft import PeftModel\n", 604 | "model = PeftModel.from_pretrained(model, output_dir)" 605 | ] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "metadata": { 610 | "id": "roqy_WRi1CSU" 611 | }, 612 | "source": [ 613 | "尝试与之前相同的提示:" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": null, 619 | "metadata": { 620 | "id": "GrCxouNp1CSU" 621 | }, 622 | "outputs": [], 623 | "source": [ 624 | "eval_prompt = \"\"\"You are a powerful text-to-SQL model. Your job is to answer questions about a database. You are given a question and context regarding one or more tables.\n", 625 | "\n", 626 | "You must output the SQL query that answers the question.\n", 627 | "### Input:\n", 628 | "Which Class has a Frequency MHz larger than 91.5, and a City of license of hyannis, nebraska?\n", 629 | "\n", 630 | "### Context:\n", 631 | "CREATE TABLE table_name_12 (class VARCHAR, frequency_mhz VARCHAR, city_of_license VARCHAR)\n", 632 | "\n", 633 | "### Response:\n", 634 | "\"\"\"\n", 635 | "\n", 636 | "model_input = tokenizer(eval_prompt, return_tensors=\"pt\").to(\"cuda\")\n", 637 | "\n", 638 | "model.eval()\n", 639 | "with torch.no_grad():\n", 640 | " print(tokenizer.decode(model.generate(**model_input, max_new_tokens=100)[0], skip_special_tokens=True))\n" 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": { 646 | "id": "I6IcTOCq1CSU" 647 | }, 648 | "source": [ 649 | "模型输出:\n", 650 | "```\n", 651 | "SELECT class FROM table_name_12 WHERE frequency_mhz > 91.5 AND city_of_license = \"hyannis, nebraska\"\n", 652 | "```\n", 653 | "从运行结果可以看到微调是有效果的!也可以将此适配器转换为 Llama.cpp 模型以在本地运行。\n" 654 | ] 655 | } 656 | ], 657 | "metadata": { 658 | "accelerator": "GPU", 659 | "colab": { 660 | "gpuType": "T4", 661 | "provenance": [], 662 | "include_colab_link": true 663 | }, 664 | "kernelspec": { 665 | "display_name": "Python 3", 666 | "name": "python3" 667 | }, 668 | "language_info": { 669 | "codemirror_mode": { 670 | "name": "ipython", 671 | "version": 3 672 | }, 673 | "file_extension": ".py", 674 | "mimetype": "text/x-python", 675 | "name": "python", 676 | "nbconvert_exporter": "python", 677 | "pygments_lexer": "ipython3", 678 | "version": "3.10.12" 679 | }, 680 | "orig_nbformat": 4 681 | }, 682 | "nbformat": 4, 683 | "nbformat_minor": 0 684 | } -------------------------------------------------------------------------------- /gpt-author/gpt_author_v2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "Ih8GSkb28daX" 17 | }, 18 | "source": [ 19 | "# gpt-author v2\n", 20 | "By Matt Shumer (https://twitter.com/mattshumer_)\n", 21 | "\n", 22 | "Github repo: https://github.com/mshumer/gpt-author\n", 23 | "\n", 24 | "只需几分钟即可生成整本小说,并自动打包为电子书。\n", 25 | "\n", 26 | "\n", 27 | "To generate a book:\n", 28 | "\n", 29 | "1、在第一个单元格中,添加 OpenAI、Stability 和(可选的 Anthropic)密钥(有关获取它们的说明,请参阅第一个单元格)。\n", 30 | "\n", 31 | "2、在最后一个单元格中填写提示、章节数和写作风格。\n", 32 | "\n", 33 | "3、运行所有单元格!一段时间后,您的 EPUB 文件应该出现在文件系统中。\n", 34 | "\n", 35 | "\n", 36 | "\n" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": { 43 | "id": "HTGF4Bw6Hnuz" 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "!pip install openai\n", 48 | "!pip install EbookLib\n", 49 | "!pip install anthropic\n", 50 | "\n", 51 | "import openai\n", 52 | "import os\n", 53 | "from ebooklib import epub\n", 54 | "import base64\n", 55 | "import os\n", 56 | "import requests\n", 57 | "\n", 58 | "openai.api_key = \"YOUR OPENAI KEY\" # get it at https://platform.openai.com/\n", 59 | "stability_api_key = \"YOUR STABILITY KEY\" # get it at https://beta.dreamstudio.ai/\n", 60 | "anthropic_api_key = \"YOUR ANTHROPIC API KEY\" # optional, if you don't add it, keep it as \"YOUR ANTHROPIC API KEY\"\n", 61 | "\n", 62 | "if anthropic_api_key != \"YOUR ANTHROPIC API KEY\":\n", 63 | " claude_true = True\n", 64 | "else:\n", 65 | " claude_true = False" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": { 72 | "id": "__nvOnmhPqwy" 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "def generate_cover_prompt(plot):\n", 77 | " response = openai.ChatCompletion.create(\n", 78 | " model=\"gpt-3.5-turbo-16k\",\n", 79 | " messages=[\n", 80 | " {\"role\": \"system\", \"content\": \"You are a creative assistant that writes a spec for the cover art of a book, based on the book's plot.\"},\n", 81 | " {\"role\": \"user\", \"content\": f\"Plot: {plot}\\n\\n--\\n\\nDescribe the cover we should create, based on the plot. This should be two sentences long, maximum.\"}\n", 82 | " ]\n", 83 | " )\n", 84 | " return response['choices'][0]['message']['content']\n", 85 | "\n", 86 | "\n", 87 | "def create_cover_image(plot):\n", 88 | "\n", 89 | " plot = str(generate_cover_prompt(plot))\n", 90 | "\n", 91 | " engine_id = \"stable-diffusion-xl-beta-v2-2-2\"\n", 92 | " api_host = os.getenv('API_HOST', 'https://api.stability.ai')\n", 93 | " api_key = stability_api_key\n", 94 | "\n", 95 | " if api_key is None:\n", 96 | " raise Exception(\"Missing Stability API key.\")\n", 97 | "\n", 98 | " response = requests.post(\n", 99 | " f\"{api_host}/v1/generation/{engine_id}/text-to-image\",\n", 100 | " headers={\n", 101 | " \"Content-Type\": \"application/json\",\n", 102 | " \"Accept\": \"application/json\",\n", 103 | " \"Authorization\": f\"Bearer {api_key}\"\n", 104 | " },\n", 105 | " json={\n", 106 | " \"text_prompts\": [\n", 107 | " {\n", 108 | " \"text\": plot\n", 109 | " }\n", 110 | " ],\n", 111 | " \"cfg_scale\": 7,\n", 112 | " \"clip_guidance_preset\": \"FAST_BLUE\",\n", 113 | " \"height\": 768,\n", 114 | " \"width\": 512,\n", 115 | " \"samples\": 1,\n", 116 | " \"steps\": 30,\n", 117 | " },\n", 118 | " )\n", 119 | "\n", 120 | " if response.status_code != 200:\n", 121 | " raise Exception(\"Non-200 response: \" + str(response.text))\n", 122 | "\n", 123 | " data = response.json()\n", 124 | "\n", 125 | " for i, image in enumerate(data[\"artifacts\"]):\n", 126 | " with open(f\"/content/cover.png\", \"wb\") as f: # replace this if running locally, to where you store the cover file\n", 127 | " f.write(base64.b64decode(image[\"base64\"]))" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": { 134 | "id": "4w-tRWAVDf-V" 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "def create_epub(title, author, chapters, cover_image_path='cover.png'):\n", 139 | " book = epub.EpubBook()\n", 140 | "\n", 141 | " # Set metadata\n", 142 | " book.set_identifier('id123456')\n", 143 | " book.set_title(title)\n", 144 | " book.set_language('en')\n", 145 | " book.add_author(author)\n", 146 | "\n", 147 | " # Add cover image\n", 148 | " with open(cover_image_path, 'rb') as cover_file:\n", 149 | " cover_image = cover_file.read()\n", 150 | " book.set_cover('cover.png', cover_image)\n", 151 | "\n", 152 | " # Create chapters and add them to the book\n", 153 | " epub_chapters = []\n", 154 | " for i, chapter_dict in enumerate(chapters):\n", 155 | " full_chapter_title = list(chapter_dict.keys())[0]\n", 156 | " chapter_content = list(chapter_dict.values())[0]\n", 157 | " if ' - ' in full_chapter_title:\n", 158 | " chapter_title = full_chapter_title.split(' - ')[1]\n", 159 | " else:\n", 160 | " chapter_title = full_chapter_title\n", 161 | "\n", 162 | " chapter_file_name = f'chapter_{i+1}.xhtml'\n", 163 | " epub_chapter = epub.EpubHtml(title=chapter_title, file_name=chapter_file_name, lang='en')\n", 164 | "\n", 165 | " # Add paragraph breaks\n", 166 | " formatted_content = ''.join(f'

{paragraph.strip()}

' for paragraph in chapter_content.split('\\n') if paragraph.strip())\n", 167 | "\n", 168 | " epub_chapter.content = f'

{chapter_title}

{formatted_content}'\n", 169 | " book.add_item(epub_chapter)\n", 170 | " epub_chapters.append(epub_chapter)\n", 171 | "\n", 172 | "\n", 173 | " # Define Table of Contents\n", 174 | " book.toc = (epub_chapters)\n", 175 | "\n", 176 | " # Add default NCX and Nav files\n", 177 | " book.add_item(epub.EpubNcx())\n", 178 | " book.add_item(epub.EpubNav())\n", 179 | "\n", 180 | " # Define CSS style\n", 181 | " style = '''\n", 182 | " @namespace epub \"http://www.idpf.org/2007/ops\";\n", 183 | " body {\n", 184 | " font-family: Cambria, Liberation Serif, serif;\n", 185 | " }\n", 186 | " h1 {\n", 187 | " text-align: left;\n", 188 | " text-transform: uppercase;\n", 189 | " font-weight: 200;\n", 190 | " }\n", 191 | " '''\n", 192 | "\n", 193 | " # Add CSS file\n", 194 | " nav_css = epub.EpubItem(uid=\"style_nav\", file_name=\"style/nav.css\", media_type=\"text/css\", content=style)\n", 195 | " book.add_item(nav_css)\n", 196 | "\n", 197 | " # Create spine\n", 198 | " book.spine = ['nav'] + epub_chapters\n", 199 | "\n", 200 | " # Save the EPUB file\n", 201 | " epub.write_epub(f'{title}.epub', book)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": { 208 | "id": "KEClRDpDK7VW" 209 | }, 210 | "outputs": [], 211 | "source": [ 212 | "import openai\n", 213 | "import random\n", 214 | "import json\n", 215 | "import ast\n", 216 | "from anthropic import Anthropic\n", 217 | "\n", 218 | "def print_step_costs(response, model):\n", 219 | " input = response['usage']['prompt_tokens']\n", 220 | " output = response['usage']['completion_tokens']\n", 221 | "\n", 222 | " if model == \"gpt-4\" or model == \"gpt-4\":\n", 223 | " input_per_token = 0.00003\n", 224 | " output_per_token = 0.00006\n", 225 | " if model == \"gpt-3.5-turbo-16k\":\n", 226 | " input_per_token = 0.000003\n", 227 | " output_per_token = 0.000004\n", 228 | " if model == \"gpt-4-32k\" or model == \"gpt-4-32k\":\n", 229 | " input_per_token = 0.00006\n", 230 | " output_per_token = 0.00012\n", 231 | " if model == \"gpt-3.5-turbo\" or model == \"gpt-3.5-turbo\":\n", 232 | " input_per_token = 0.0000015\n", 233 | " output_per_token = 0.000002\n", 234 | " if model == \"claude-2\":\n", 235 | " input_per_token = 0.00001102\n", 236 | " output_per_token = 0.00003268\n", 237 | "\n", 238 | " input_cost = int(input) * input_per_token\n", 239 | " output_cost = int(output) * output_per_token\n", 240 | "\n", 241 | " total_cost = input_cost + output_cost\n", 242 | " print('Step Cost (OpenAI):', total_cost)\n", 243 | "\n", 244 | "def print_step_costs_anthropic(prompt, response):\n", 245 | " client = Anthropic()\n", 246 | " in_tokens = client.count_tokens(prompt)\n", 247 | " out_tokens = client.count_tokens(response)\n", 248 | "\n", 249 | " input_cost = 0.00001102 * in_tokens\n", 250 | " output_cost = 0.00003268 * out_tokens\n", 251 | "\n", 252 | " total_cost = input_cost + output_cost\n", 253 | " print('Step Cost (Anthropic):', total_cost)\n", 254 | "\n", 255 | "def generate_plots(prompt):\n", 256 | " response = openai.ChatCompletion.create(\n", 257 | " model=\"gpt-4\",\n", 258 | " messages=[\n", 259 | " {\"role\": \"system\", \"content\": \"You are a creative assistant that generates engaging fantasy novel plots.\"},\n", 260 | " {\"role\": \"user\", \"content\": f\"Generate 10 fantasy novel plots based on this prompt: {prompt}\"}\n", 261 | " ]\n", 262 | " )\n", 263 | "\n", 264 | " print_step_costs(response, \"gpt-4\")\n", 265 | "\n", 266 | " return response['choices'][0]['message']['content'].split('\\n')\n", 267 | "\n", 268 | "def select_most_engaging(plots):\n", 269 | " response = openai.ChatCompletion.create(\n", 270 | " model=\"gpt-4\",\n", 271 | " messages=[\n", 272 | " {\"role\": \"system\", \"content\": \"You are an expert in writing fantastic fantasy novel plots.\"},\n", 273 | " {\"role\": \"user\", \"content\": f\"Here are a number of possible plots for a new novel: {plots}\\n\\n--\\n\\nNow, write the final plot that we will go with. It can be one of these, a mix of the best elements of multiple, or something completely new and better. The most important thing is the plot should be fantastic, unique, and engaging.\"}\n", 274 | " ]\n", 275 | " )\n", 276 | "\n", 277 | " print_step_costs(response, \"gpt-4\")\n", 278 | "\n", 279 | " return response['choices'][0]['message']['content']\n", 280 | "\n", 281 | "def improve_plot(plot):\n", 282 | " response = openai.ChatCompletion.create(\n", 283 | " model=\"gpt-4\",\n", 284 | " messages=[\n", 285 | " {\"role\": \"system\", \"content\": \"You are an expert in improving and refining story plots.\"},\n", 286 | " {\"role\": \"user\", \"content\": f\"Improve this plot: {plot}\"}\n", 287 | " ]\n", 288 | " )\n", 289 | "\n", 290 | " print_step_costs(response, \"gpt-4\")\n", 291 | "\n", 292 | " return response['choices'][0]['message']['content']\n", 293 | "\n", 294 | "def get_title(plot):\n", 295 | " response = openai.ChatCompletion.create(\n", 296 | " model=\"gpt-3.5-turbo-16k\",\n", 297 | " messages=[\n", 298 | " {\"role\": \"system\", \"content\": \"You are an expert writer.\"},\n", 299 | " {\"role\": \"user\", \"content\": f\"Here is the plot: {plot}\\n\\nWhat is the title of this book? Just respond with the title, do nothing else.\"}\n", 300 | " ]\n", 301 | " )\n", 302 | "\n", 303 | " print_step_costs(response, \"gpt-3.5-turbo-16k\")\n", 304 | "\n", 305 | " return response['choices'][0]['message']['content']\n", 306 | "\n", 307 | "def write_first_chapter(plot, first_chapter_title, writing_style, claude=True):\n", 308 | " if claude:\n", 309 | " url = \"https://api.anthropic.com/v1/complete\"\n", 310 | "\n", 311 | " headers = {\n", 312 | " \"anthropic-version\": \"2023-06-01\",\n", 313 | " \"content-type\": \"application/json\",\n", 314 | " \"x-api-key\": anthropic_api_key,\n", 315 | " }\n", 316 | "\n", 317 | " prompt_one = f\"\\n\\nHuman: You are a world-class fantasy writer. I will give you the title of a novel, a high-level plot to follow, and a desired writing style to use. From the title, plot, and writing style, write the first chapter of the novel. Make it incredibly unique, engaging, and well-written. Start it off with a bang, and include dialogue. Include only the chapter text, and no surrounding explanations or text. Do you understand?\\n\\nAssistant: Yes, I understand. Please provide the title, plot, and writing style, and I will write a fantastic opening chapter with dialogue that will hook the reader.\\n\\nHuman: Here is the high-level plot to follow: {plot}\\n\\The title of the novel is: `{first_chapter_title}`.\\n\\nHere is a description of the writing style you should use: `{writing_style}`\\n\\nWrite the first chapter please!\\n\\nAssistant: Okay, I've got a really exciting first chapter for you. It's twenty paragraphs long and very well-written. As you can see, the language I use is very understandable — I avoided using overly complex words and phrases:\\n\\nTitle: {first_chapter_title}\\n\\nChapter #1 Text```\"\n", 318 | "\n", 319 | " data = {\n", 320 | " \"model\": \"claude-2\",\n", 321 | " \"prompt\": prompt_one,\n", 322 | " \"max_tokens_to_sample\": 5000,\n", 323 | " }\n", 324 | "\n", 325 | " response = requests.post(url, headers=headers, json=data)\n", 326 | "\n", 327 | " initial_first_chapter = response.json()['completion'].strip().split('```')[0].strip()\n", 328 | "\n", 329 | " print_step_costs_anthropic(prompt_one, response.json()['completion'])\n", 330 | "\n", 331 | " prompt_two = f\"\\n\\nHuman: You are a world-class fantasy writer. Your job is to take your student's rough initial draft of the first chapter of their fantasy novel, and rewrite it to be significantly better, with much more detail. Do you understand?\\n\\nAssistant: Yes, I understand. Please provide the plot and the student-written chapter, and I will rewrite the chapter in a far superior way.\\n\\nHuman: Here is the high-level plot you asked your student to follow: {plot}\\n\\nHere is the first chapter they wrote: {initial_first_chapter}\\n\\nNow, rewrite the first chapter of this novel, in a way that is far superior to your student's chapter. It should still follow the exact same plot, but it should be far more detailed, much longer, and more engaging. Here is a description of the writing style you should use: `{writing_style}`\\n\\nAssistant: Okay, I've rewritten the first chapter. I took great care to improve it. While the plot is the same, you can see that my version is noticeably longer, easier to read, and more exciting. Also, the language I used is far more accessible to a broader audience.\\n\\n```\"\n", 332 | " data = {\n", 333 | " \"model\": \"claude-2\",\n", 334 | " \"prompt\": prompt_two,\n", 335 | " \"max_tokens_to_sample\": 5000,\n", 336 | " }\n", 337 | "\n", 338 | " response_improved = requests.post(url, headers=headers, json=data)\n", 339 | "\n", 340 | " print_step_costs_anthropic(prompt_two, response_improved.json()['completion'])\n", 341 | "\n", 342 | " return response_improved.json()['completion'].strip().split('```')[0].strip()\n", 343 | "\n", 344 | "\n", 345 | " else:\n", 346 | " response = openai.ChatCompletion.create(\n", 347 | " model=\"gpt-4\",\n", 348 | " messages=[\n", 349 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer.\"},\n", 350 | " {\"role\": \"user\", \"content\": f\"Here is the high-level plot to follow: {plot}\\n\\nWrite the first chapter of this novel: `{first_chapter_title}`.\\n\\nMake it incredibly unique, engaging, and well-written.\\n\\nHere is a description of the writing style you should use: `{writing_style}`\\n\\nInclude only the chapter text. There is no need to rewrite the chapter name.\"}\n", 351 | " ]\n", 352 | " )\n", 353 | "\n", 354 | " print_step_costs(response, \"gpt-4\")\n", 355 | "\n", 356 | " improved_response = openai.ChatCompletion.create(\n", 357 | " model=\"gpt-4-32k\",\n", 358 | " messages=[\n", 359 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer. Your job is to take your student's rough initial draft of the first chapter of their fantasy novel, and rewrite it to be significantly better, with much more detail.\"},\n", 360 | " {\"role\": \"user\", \"content\": f\"Here is the high-level plot you asked your student to follow: {plot}\\n\\nHere is the first chapter they wrote: {response['choices'][0]['message']['content']}\\n\\nNow, rewrite the first chapter of this novel, in a way that is far superior to your student's chapter. It should still follow the exact same plot, but it should be far more detailed, much longer, and more engaging. Here is a description of the writing style you should use: `{writing_style}`\"}\n", 361 | " ]\n", 362 | " )\n", 363 | "\n", 364 | " print_step_costs(response, \"gpt-4-32k\")\n", 365 | "\n", 366 | " return improved_response['choices'][0]['message']['content']\n", 367 | "\n", 368 | "\n", 369 | "def write_chapter(previous_chapters, plot, chapter_title, claude=True):\n", 370 | " if claude:\n", 371 | " url = \"https://api.anthropic.com/v1/complete\"\n", 372 | "\n", 373 | " headers = {\n", 374 | " \"anthropic-version\": \"2023-06-01\",\n", 375 | " \"content-type\": \"application/json\",\n", 376 | " \"x-api-key\": anthropic_api_key,\n", 377 | " }\n", 378 | "\n", 379 | " prompt = f\"\\n\\nHuman: You are a world-class fantasy writer. I will provide you with the plot of the novel, the previous chapters, and the plan for the next chapter. Your task is to write the next chapter of the novel, following the plot and taking in the previous chapters as context. Do you understand?\\n\\nAssistant: Yes, I understand. You want me to write the next chapter of a novel, using the plot you provide, the previous chapters for context, and a specific plan for the next chapter. I will ensure the chapter is beautifully written and I will not rewrite the chapter name.\\n\\nHuman: That's correct. Here is the plot: {plot}\\n\\nHere are the previous chapters: {previous_chapters}\\n\\nHere is the plan for the next chapter: {chapter_title}\\n\\nWrite it beautifully. Include only the chapter text. There is no need to rewrite the chapter name.\\n\\nAssistant: Here is the next chapter. As you can see, it's around the same length as the previous chapters, and contains witty dialogue:\\n```Chapter\"\n", 380 | "\n", 381 | " data = {\n", 382 | " \"model\": \"claude-2\",\n", 383 | " \"prompt\": prompt,\n", 384 | " \"max_tokens_to_sample\": 5000,\n", 385 | " }\n", 386 | "\n", 387 | " response = requests.post(url, headers=headers, json=data)\n", 388 | "\n", 389 | " print_step_costs_anthropic(prompt, response.json()['completion'])\n", 390 | "\n", 391 | " return 'Chapter ' + response.json()['completion'].strip().split('```')[0].strip()\n", 392 | " else:\n", 393 | " try:\n", 394 | " i = random.randint(1,2242)\n", 395 | " response = openai.ChatCompletion.create(\n", 396 | " model=\"gpt-4\",\n", 397 | " messages=[\n", 398 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer.\"},\n", 399 | " {\"role\": \"user\", \"content\": f\"Plot: {plot}, Previous Chapters: {previous_chapters}\\n\\n--\\n\\nWrite the next chapter of this novel, following the plot and taking in the previous chapters as context. Here is the plan for this chapter: {chapter_title}\\n\\nWrite it beautifully. Include only the chapter text. There is no need to rewrite the chapter name.\"}\n", 400 | " ]\n", 401 | " )\n", 402 | "\n", 403 | " print_step_costs(response, \"gpt-4\")\n", 404 | "\n", 405 | " return response['choices'][0]['message']['content']\n", 406 | " except:\n", 407 | " response = openai.ChatCompletion.create(\n", 408 | " model=\"gpt-4-32k\",\n", 409 | " messages=[\n", 410 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer.\"},\n", 411 | " {\"role\": \"user\", \"content\": f\"Plot: {plot}, Previous Chapters: {previous_chapters}\\n\\n--\\n\\nWrite the next chapter of this novel, following the plot and taking in the previous chapters as context. Here is the plan for this chapter: {chapter_title}\\n\\nWrite it beautifully. Include only the chapter text. There is no need to rewrite the chapter name.\"}\n", 412 | " ]\n", 413 | " )\n", 414 | "\n", 415 | " print_step_costs(response, \"gpt-4-32k\")\n", 416 | "\n", 417 | " return response['choices'][0]['message']['content']\n", 418 | "\n", 419 | "\n", 420 | "def generate_storyline(prompt, num_chapters):\n", 421 | " print(\"Generating storyline with chapters and high-level details...\")\n", 422 | " json_format = \"\"\"[{\"Chapter CHAPTER_NUMBER_HERE - CHAPTER_TITLE_GOES_HERE\": \"CHAPTER_OVERVIEW_AND_DETAILS_GOES_HERE\"}, ...]\"\"\"\n", 423 | " response = openai.ChatCompletion.create(\n", 424 | " model=\"gpt-4\",\n", 425 | " messages=[\n", 426 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer. Your job is to write a detailed storyline, complete with chapters, for a fantasy novel. Don't be flowery -- you want to get the message across in as few words as possible. But those words should contain lots of information.\"},\n", 427 | " {\"role\": \"user\", \"content\": f'Write a fantastic storyline with {num_chapters} chapters and high-level details based on this plot: {prompt}.\\n\\nDo it in this list of dictionaries format {json_format}'}\n", 428 | " ]\n", 429 | " )\n", 430 | "\n", 431 | " print_step_costs(response, \"gpt-4\")\n", 432 | "\n", 433 | " improved_response = openai.ChatCompletion.create(\n", 434 | " model=\"gpt-4\",\n", 435 | " messages=[\n", 436 | " {\"role\": \"system\", \"content\": \"You are a world-class fantasy writer. Your job is to take your student's rough initial draft of the storyline of a fantasy novel, and rewrite it to be significantly better.\"},\n", 437 | " {\"role\": \"user\", \"content\": f\"Here is the draft storyline they wrote: {response['choices'][0]['message']['content']}\\n\\nNow, rewrite the storyline, in a way that is far superior to your student's version. It should have the same number of chapters, but it should be much improved in as many ways as possible. Remember to do it in this list of dictionaries format {json_format}\"}\n", 438 | " ]\n", 439 | " )\n", 440 | "\n", 441 | " print_step_costs(improved_response, \"gpt-4\")\n", 442 | "\n", 443 | " return improved_response['choices'][0]['message']['content']\n", 444 | "\n", 445 | "\n", 446 | "def write_to_file(prompt, content):\n", 447 | "\n", 448 | " # Create a directory for the prompts if it doesn't exist\n", 449 | " if not os.path.exists('prompts'):\n", 450 | " os.mkdir('prompts')\n", 451 | "\n", 452 | " # Replace invalid characters for filenames\n", 453 | " valid_filename = ''.join(c for c in prompt if c.isalnum() or c in (' ', '.', '_')).rstrip()\n", 454 | " file_path = f'prompts/{valid_filename}.txt'\n", 455 | "\n", 456 | " with open(file_path, 'w', encoding='utf-8') as f:\n", 457 | " f.write(content)\n", 458 | "\n", 459 | " print(f'Output for prompt \"{prompt}\" has been written to {file_path}\\n')\n", 460 | "\n", 461 | "\n", 462 | "def write_fantasy_novel(prompt, num_chapters, writing_style, claude_true=False):\n", 463 | " plots = generate_plots(prompt)\n", 464 | " print('generated plots')\n", 465 | "\n", 466 | " best_plot = select_most_engaging(plots)\n", 467 | " print('selected best plot')\n", 468 | "\n", 469 | " improved_plot = improve_plot(best_plot)\n", 470 | " print('plot improved')\n", 471 | "\n", 472 | " title = get_title(improved_plot)\n", 473 | " print('title generated')\n", 474 | "\n", 475 | " storyline = generate_storyline(improved_plot, num_chapters)\n", 476 | " print('storyline generated')\n", 477 | " chapter_titles = ast.literal_eval(storyline)\n", 478 | "\n", 479 | "\n", 480 | " novel = f\"Storyline:\\n{storyline}\\n\\n\"\n", 481 | "\n", 482 | " first_chapter = write_first_chapter(storyline, chapter_titles[0], writing_style.strip(), claude_true)\n", 483 | " print('first chapter written')\n", 484 | " novel += f\"Chapter 1:\\n{first_chapter}\\n\"\n", 485 | " chapters = [first_chapter]\n", 486 | "\n", 487 | " for i in range(num_chapters - 1):\n", 488 | " print(f\"Writing chapter {i+2}...\") # + 2 because the first chapter was already added\n", 489 | "\n", 490 | " chapter = write_chapter(novel, storyline, chapter_titles[i+1])\n", 491 | " try:\n", 492 | " if len(str(chapter)) < 100:\n", 493 | " print('Length minimum not hit. Trying again.')\n", 494 | " chapter = write_chapter(novel, storyline, chapter_titles[i+1])\n", 495 | " except:\n", 496 | " chapter = write_chapter(novel, storyline, chapter_titles[i+1])\n", 497 | "\n", 498 | " novel += f\"Chapter {i+2}:\\n{chapter}\\n\"\n", 499 | " chapters.append(chapter)\n", 500 | "\n", 501 | " return novel, title, chapters, chapter_titles" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": null, 507 | "metadata": { 508 | "id": "IZeHy7qyACCb" 509 | }, 510 | "outputs": [], 511 | "source": [ 512 | "# Example usage:\n", 513 | "prompt = \"A kingdom hidden deep in the forest, where every tree is a portal to another world.\"\n", 514 | "num_chapters = 10\n", 515 | "writing_style = \"Clear and easily understandable, similar to a young adult novel. Lots of dialogue.\"\n", 516 | "novel, title, chapters, chapter_titles = write_fantasy_novel(prompt, num_chapters, writing_style, claude_true)\n", 517 | "\n", 518 | "# Replace chapter descriptions with body text in chapter_titles\n", 519 | "for i, chapter in enumerate(chapters):\n", 520 | " chapter_number_and_title = list(chapter_titles[i].keys())[0]\n", 521 | " chapter_titles[i] = {chapter_number_and_title: chapter}\n", 522 | "\n", 523 | "# Create the cover\n", 524 | "create_cover_image(str(chapter_titles))\n", 525 | "\n", 526 | "# Create the EPUB file\n", 527 | "create_epub(title, 'AI', chapter_titles, '/content/cover.png')" 528 | ] 529 | } 530 | ], 531 | "metadata": { 532 | "colab": { 533 | "provenance": [], 534 | "include_colab_link": true 535 | }, 536 | "kernelspec": { 537 | "display_name": "Python 3", 538 | "name": "python3" 539 | }, 540 | "language_info": { 541 | "name": "python" 542 | } 543 | }, 544 | "nbformat": 4, 545 | "nbformat_minor": 0 546 | } -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_Database/Langchain_Agents_SQL_Database_Agent.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "include_colab_link": true 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "view-in-github", 22 | "colab_type": "text" 23 | }, 24 | "source": [ 25 | "\"Open" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "source": [ 31 | "!pip install langchain openai pymysql --upgrade -q" 32 | ], 33 | "metadata": { 34 | "id": "01A-AWhxPka9", 35 | "colab": { 36 | "base_uri": "https://localhost:8080/" 37 | }, 38 | "outputId": "41670f21-4f58-401c-a8bb-b29ae769e6a3" 39 | }, 40 | "execution_count": 1, 41 | "outputs": [ 42 | { 43 | "output_type": "stream", 44 | "name": "stdout", 45 | "text": [ 46 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m21.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 47 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 48 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.7/43.7 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 49 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m73.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 50 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m10.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 51 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m15.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 52 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 53 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m18.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 54 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 55 | "\u001b[?25h" 56 | ] 57 | } 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "source": [ 63 | "##Agents" 64 | ], 65 | "metadata": { 66 | "id": "JWDoW0HUhfxL" 67 | } 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "source": [ 72 | "https://python.langchain.com/en/latest/modules/agents/getting_started.html\n", 73 | "\n" 74 | ], 75 | "metadata": { 76 | "id": "rrH3mfgchyyT" 77 | } 78 | }, 79 | { 80 | "cell_type": "code", 81 | "source": [ 82 | "import os\n", 83 | "os.environ['OPENAI_API_KEY'] = \"your_openai_api_key\"\n", 84 | "os.environ[\"SERPAPI_API_KEY\"] = \"your_serpapi_api_key\"" 85 | ], 86 | "metadata": { 87 | "id": "KHHo_6htR1GA" 88 | }, 89 | "execution_count": 7, 90 | "outputs": [] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "source": [ 95 | "!pip install google-search-results -q" 96 | ], 97 | "metadata": { 98 | "id": "_brJP_HbhW3K", 99 | "colab": { 100 | "base_uri": "https://localhost:8080/" 101 | }, 102 | "outputId": "b5eb21ee-ea90-4881-dfb1-e6d73634f1e2" 103 | }, 104 | "execution_count": 3, 105 | "outputs": [ 106 | { 107 | "output_type": "stream", 108 | "name": "stdout", 109 | "text": [ 110 | " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", 111 | " Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n" 112 | ] 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "source": [ 119 | "https://python.langchain.com/en/latest/modules/agents/agents/agent_types.html" 120 | ], 121 | "metadata": { 122 | "id": "7PLCAVpKj2eU" 123 | } 124 | }, 125 | { 126 | "cell_type": "code", 127 | "source": [ 128 | "from langchain.agents import load_tools\n", 129 | "from langchain.agents import initialize_agent\n", 130 | "from langchain.agents import AgentType\n", 131 | "from langchain.llms import OpenAI" 132 | ], 133 | "metadata": { 134 | "id": "66hNmNKMhd5n" 135 | }, 136 | "execution_count": 9, 137 | "outputs": [] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "source": [ 142 | "llm = OpenAI(temperature=0)\n", 143 | "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)" 144 | ], 145 | "metadata": { 146 | "id": "CZVJLedJhj9A" 147 | }, 148 | "execution_count": 10, 149 | "outputs": [] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "source": [ 154 | "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)\n", 155 | "agent.run(\" OpenAI的老板是谁,他的年龄的0.5次方是多少?\")" 156 | ], 157 | "metadata": { 158 | "colab": { 159 | "base_uri": "https://localhost:8080/", 160 | "height": 35 161 | }, 162 | "id": "3z_hnwRQkHlN", 163 | "outputId": "dbbe793f-1201-4186-a5ac-0640c71e49c0" 164 | }, 165 | "execution_count": 13, 166 | "outputs": [ 167 | { 168 | "output_type": "execute_result", 169 | "data": { 170 | "text/plain": [ 171 | "'6.164414002968976'" 172 | ], 173 | "application/vnd.google.colaboratory.intrinsic+json": { 174 | "type": "string" 175 | } 176 | }, 177 | "metadata": {}, 178 | "execution_count": 13 179 | } 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "source": [ 185 | "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n", 186 | "agent.run(\"OpenAI的老板是谁,他的年龄的0.5次方是多少?\")" 187 | ], 188 | "metadata": { 189 | "colab": { 190 | "base_uri": "https://localhost:8080/", 191 | "height": 375 192 | }, 193 | "id": "Z1obLhbZkR05", 194 | "outputId": "06a4b6bc-8c4e-43eb-bbbe-e84c022d41ae" 195 | }, 196 | "execution_count": 14, 197 | "outputs": [ 198 | { 199 | "output_type": "stream", 200 | "name": "stdout", 201 | "text": [ 202 | "\n", 203 | "\n", 204 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 205 | "\u001b[32;1m\u001b[1;3m I need to find out who the CEO of OpenAI is and then calculate their age to the power of 0.5.\n", 206 | "Action: Search\n", 207 | "Action Input: \"OpenAI CEO\"\u001b[0m\n", 208 | "Observation: \u001b[36;1m\u001b[1;3mSam Altman, the CEO of ChatGPT maker OpenAI, used a high-profile trip to South Korea on Friday to call for coordinated international ...\u001b[0m\n", 209 | "Thought:\u001b[32;1m\u001b[1;3m I need to find out the age of the CEO\n", 210 | "Action: Search\n", 211 | "Action Input: \"Sam Altman age\"\u001b[0m\n", 212 | "Observation: \u001b[36;1m\u001b[1;3m38 years\u001b[0m\n", 213 | "Thought:\u001b[32;1m\u001b[1;3m I now know the age of the CEO and need to calculate it to the power of 0.5\n", 214 | "Action: Calculator\n", 215 | "Action Input: 38^0.5\u001b[0m\n", 216 | "Observation: \u001b[33;1m\u001b[1;3mAnswer: 6.164414002968976\u001b[0m\n", 217 | "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", 218 | "Final Answer: 6.164414002968976\u001b[0m\n", 219 | "\n", 220 | "\u001b[1m> Finished chain.\u001b[0m\n" 221 | ] 222 | }, 223 | { 224 | "output_type": "execute_result", 225 | "data": { 226 | "text/plain": [ 227 | "'6.164414002968976'" 228 | ], 229 | "application/vnd.google.colaboratory.intrinsic+json": { 230 | "type": "string" 231 | } 232 | }, 233 | "metadata": {}, 234 | "execution_count": 14 235 | } 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "source": [ 241 | "https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html" 242 | ], 243 | "metadata": { 244 | "id": "_2VxCUB2h7Gl" 245 | } 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": { 251 | "id": "-ewK38IAPhsU" 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "from langchain.agents import create_sql_agent\n", 256 | "from langchain.agents.agent_toolkits import SQLDatabaseToolkit\n", 257 | "from langchain.sql_database import SQLDatabase\n", 258 | "from langchain.llms.openai import OpenAI\n", 259 | "from langchain.agents import AgentExecutor" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "source": [ 265 | "https://www.mysqltutorial.org/mysql-sample-database.aspx" 266 | ], 267 | "metadata": { 268 | "id": "dhr58gI-Auu_" 269 | } 270 | }, 271 | { 272 | "cell_type": "code", 273 | "source": [ 274 | "db_user = \"db_user\"\n", 275 | "db_password = \"db_password\"\n", 276 | "db_host = \"db_host\"\n", 277 | "db_name = \"db_name\"\n", 278 | "db = SQLDatabase.from_uri(f\"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}\")" 279 | ], 280 | "metadata": { 281 | "id": "M8f9qz6GPmO8" 282 | }, 283 | "execution_count": null, 284 | "outputs": [] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "source": [ 289 | "# llm=OpenAI(temperature=0)\n", 290 | "from langchain.chat_models import ChatOpenAI\n", 291 | "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\")\n", 292 | "# chat = ChatOpenAI(model_name=\"gpt-4\")" 293 | ], 294 | "metadata": { 295 | "id": "8Ca9LSrpbhFu" 296 | }, 297 | "execution_count": null, 298 | "outputs": [] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "source": [ 303 | "toolkit = SQLDatabaseToolkit(db=db)\n", 304 | "\n", 305 | "agent_executor = create_sql_agent(\n", 306 | " llm=llm,\n", 307 | " toolkit=toolkit,\n", 308 | " verbose=True\n", 309 | ")" 310 | ], 311 | "metadata": { 312 | "id": "jJJuOzhAP4Ob" 313 | }, 314 | "execution_count": null, 315 | "outputs": [] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "source": [ 320 | "###Describing a table" 321 | ], 322 | "metadata": { 323 | "id": "PZkzmH8ee49i" 324 | } 325 | }, 326 | { 327 | "cell_type": "code", 328 | "source": [ 329 | "agent_executor.run(\"描述与订单相关的表及其关系\")" 330 | ], 331 | "metadata": { 332 | "colab": { 333 | "base_uri": "https://localhost:8080/", 334 | "height": 1000 335 | }, 336 | "id": "5wyZzuT9BqCf", 337 | "outputId": "06b6b6c6-55f9-4957-e467-2a0a012dccd0" 338 | }, 339 | "execution_count": null, 340 | "outputs": [ 341 | { 342 | "output_type": "stream", 343 | "name": "stdout", 344 | "text": [ 345 | "\n", 346 | "\n", 347 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 348 | "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n", 349 | "Action Input: \"\"\u001b[0m\n", 350 | "Observation: \u001b[38;5;200m\u001b[1;3mcustomers, employees, payments, products, productlines, orderdetails, offices, orders\u001b[0m\n", 351 | "Thought:\u001b[32;1m\u001b[1;3mI should query the orderdetails table to see how it is related to the orders table.\n", 352 | "Action: schema_sql_db\n", 353 | "Action Input: \"orders, orderdetails\"\u001b[0m\n", 354 | "Observation: \u001b[33;1m\u001b[1;3m\n", 355 | "CREATE TABLE orders (\n", 356 | "\t`orderNumber` INTEGER NOT NULL, \n", 357 | "\t`orderDate` DATE NOT NULL, \n", 358 | "\t`requiredDate` DATE NOT NULL, \n", 359 | "\t`shippedDate` DATE, \n", 360 | "\tstatus VARCHAR(15) NOT NULL, \n", 361 | "\tcomments TEXT, \n", 362 | "\t`customerNumber` INTEGER NOT NULL, \n", 363 | "\tPRIMARY KEY (`orderNumber`), \n", 364 | "\tCONSTRAINT orders_ibfk_1 FOREIGN KEY(`customerNumber`) REFERENCES customers (`customerNumber`)\n", 365 | ")ENGINE=InnoDB DEFAULT CHARSET=latin1\n", 366 | "\n", 367 | "/*\n", 368 | "3 rows from orders table:\n", 369 | "orderNumber\torderDate\trequiredDate\tshippedDate\tstatus\tcomments\tcustomerNumber\n", 370 | "10100\t2003-01-06\t2003-01-13\t2003-01-10\tShipped\tNone\t363\n", 371 | "10101\t2003-01-09\t2003-01-18\t2003-01-11\tShipped\tCheck on availability.\t128\n", 372 | "10102\t2003-01-10\t2003-01-18\t2003-01-14\tShipped\tNone\t181\n", 373 | "*/\n", 374 | "\n", 375 | "\n", 376 | "CREATE TABLE orderdetails (\n", 377 | "\t`orderNumber` INTEGER NOT NULL, \n", 378 | "\t`productCode` VARCHAR(15) NOT NULL, \n", 379 | "\t`quantityOrdered` INTEGER NOT NULL, \n", 380 | "\t`priceEach` DECIMAL(10, 2) NOT NULL, \n", 381 | "\t`orderLineNumber` SMALLINT NOT NULL, \n", 382 | "\tPRIMARY KEY (`orderNumber`, `productCode`), \n", 383 | "\tCONSTRAINT orderdetails_ibfk_1 FOREIGN KEY(`orderNumber`) REFERENCES orders (`orderNumber`), \n", 384 | "\tCONSTRAINT orderdetails_ibfk_2 FOREIGN KEY(`productCode`) REFERENCES products (`productCode`)\n", 385 | ")ENGINE=InnoDB DEFAULT CHARSET=latin1\n", 386 | "\n", 387 | "/*\n", 388 | "3 rows from orderdetails table:\n", 389 | "orderNumber\tproductCode\tquantityOrdered\tpriceEach\torderLineNumber\n", 390 | "10100\tS18_1749\t30\t136.00\t3\n", 391 | "10100\tS18_2248\t50\t55.09\t2\n", 392 | "10100\tS18_4409\t22\t75.46\t4\n", 393 | "*/\u001b[0m\n", 394 | "Thought:\u001b[32;1m\u001b[1;3mThe orderdetails table has a foreign key constraint on the orderNumber column that references the orders table. I can use this to join the two tables and describe the columns in the orderdetails table.\n", 395 | "Action: schema_sql_db\n", 396 | "Action Input: \"orderdetails\"\u001b[0m\n", 397 | "Observation: \u001b[33;1m\u001b[1;3m\n", 398 | "CREATE TABLE orderdetails (\n", 399 | "\t`orderNumber` INTEGER NOT NULL, \n", 400 | "\t`productCode` VARCHAR(15) NOT NULL, \n", 401 | "\t`quantityOrdered` INTEGER NOT NULL, \n", 402 | "\t`priceEach` DECIMAL(10, 2) NOT NULL, \n", 403 | "\t`orderLineNumber` SMALLINT NOT NULL, \n", 404 | "\tPRIMARY KEY (`orderNumber`, `productCode`), \n", 405 | "\tCONSTRAINT orderdetails_ibfk_1 FOREIGN KEY(`orderNumber`) REFERENCES orders (`orderNumber`), \n", 406 | "\tCONSTRAINT orderdetails_ibfk_2 FOREIGN KEY(`productCode`) REFERENCES products (`productCode`)\n", 407 | ")ENGINE=InnoDB DEFAULT CHARSET=latin1\n", 408 | "\n", 409 | "/*\n", 410 | "3 rows from orderdetails table:\n", 411 | "orderNumber\tproductCode\tquantityOrdered\tpriceEach\torderLineNumber\n", 412 | "10100\tS18_1749\t30\t136.00\t3\n", 413 | "10100\tS18_2248\t50\t55.09\t2\n", 414 | "10100\tS18_4409\t22\t75.46\t4\n", 415 | "*/\u001b[0m\n", 416 | "Thought:\u001b[32;1m\u001b[1;3mI can use a JOIN statement to get the information from the orders table related to the orderdetails table.\n", 417 | "Action: query_checker_sql_db\n", 418 | "Action Input: \"SELECT orders.* FROM orders JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber WHERE orderdetails.orderNumber = 10100\"\u001b[0m\n", 419 | "Observation: \u001b[31;1m\u001b[1;3m\n", 420 | "\n", 421 | "SELECT orders.* FROM orders JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber WHERE orderdetails.orderNumber = 10100\u001b[0m\n", 422 | "Thought:\u001b[32;1m\u001b[1;3mThe query looks correct. I can now run it to get the answer to the original question.\n", 423 | "Action: query_sql_db\n", 424 | "Action Input: \"SELECT orders.* FROM orders JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber WHERE orderdetails.orderNumber = 10100\"\u001b[0m\n", 425 | "Observation: \u001b[36;1m\u001b[1;3m[(10100, datetime.date(2003, 1, 6), datetime.date(2003, 1, 13), datetime.date(2003, 1, 10), 'Shipped', None, 363), (10100, datetime.date(2003, 1, 6), datetime.date(2003, 1, 13), datetime.date(2003, 1, 10), 'Shipped', None, 363), (10100, datetime.date(2003, 1, 6), datetime.date(2003, 1, 13), datetime.date(2003, 1, 10), 'Shipped', None, 363), (10100, datetime.date(2003, 1, 6), datetime.date(2003, 1, 13), datetime.date(2003, 1, 10), 'Shipped', None, 363)]\u001b[0m\n", 426 | "Thought:\u001b[32;1m\u001b[1;3mThe query returned duplicate rows, but the relevant information is present. I can now give the final answer.\n", 427 | "Final Answer: The orders table and the orderdetails table are related by the orderNumber column, which serves as a foreign key in the orderdetails table referencing the primary key in the orders table. To get the information from both tables related to a specific order, you can use a JOIN statement with a WHERE clause specifying the desired orderNumber.\u001b[0m\n", 428 | "\n", 429 | "\u001b[1m> Finished chain.\u001b[0m\n" 430 | ] 431 | }, 432 | { 433 | "output_type": "execute_result", 434 | "data": { 435 | "text/plain": [ 436 | "'The orders table and the orderdetails table are related by the orderNumber column, which serves as a foreign key in the orderdetails table referencing the primary key in the orders table. To get the information from both tables related to a specific order, you can use a JOIN statement with a WHERE clause specifying the desired orderNumber.'" 437 | ], 438 | "application/vnd.google.colaboratory.intrinsic+json": { 439 | "type": "string" 440 | } 441 | }, 442 | "metadata": {}, 443 | "execution_count": 13 444 | } 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "source": [ 450 | "###Recovering from an error" 451 | ], 452 | "metadata": { 453 | "id": "AxeXiqODezwk" 454 | } 455 | }, 456 | { 457 | "cell_type": "code", 458 | "source": [ 459 | "agent_executor.run(\"描述PurchaseDetails表\")" 460 | ], 461 | "metadata": { 462 | "colab": { 463 | "base_uri": "https://localhost:8080/", 464 | "height": 767 465 | }, 466 | "id": "35oyluNYRxe_", 467 | "outputId": "c894eed4-6c8c-42d8-dcf5-8a9458460dec" 468 | }, 469 | "execution_count": null, 470 | "outputs": [ 471 | { 472 | "output_type": "stream", 473 | "name": "stdout", 474 | "text": [ 475 | "\n", 476 | "\n", 477 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 478 | "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n", 479 | "Action Input: \"\"\u001b[0m\n", 480 | "Observation: \u001b[38;5;200m\u001b[1;3mcustomers, employees, payments, products, productlines, orderdetails, offices, orders\u001b[0m\n", 481 | "Thought:\u001b[32;1m\u001b[1;3mI can query the PurchaseDetails table, but first I need to know its schema.\n", 482 | "Action: schema_sql_db\n", 483 | "Action Input: \"PurchaseDetails\"\u001b[0m\n", 484 | "Observation: \u001b[33;1m\u001b[1;3mError: table_names {'PurchaseDetails'} not found in database\u001b[0m\n", 485 | "Thought:\u001b[32;1m\u001b[1;3mOops, I made a typo in the table name. I'll try again.\n", 486 | "Action: schema_sql_db\n", 487 | "Action Input: \"orderdetails\"\u001b[0m\n", 488 | "Observation: \u001b[33;1m\u001b[1;3m\n", 489 | "CREATE TABLE orderdetails (\n", 490 | "\t`orderNumber` INTEGER NOT NULL, \n", 491 | "\t`productCode` VARCHAR(15) NOT NULL, \n", 492 | "\t`quantityOrdered` INTEGER NOT NULL, \n", 493 | "\t`priceEach` DECIMAL(10, 2) NOT NULL, \n", 494 | "\t`orderLineNumber` SMALLINT NOT NULL, \n", 495 | "\tPRIMARY KEY (`orderNumber`, `productCode`), \n", 496 | "\tCONSTRAINT orderdetails_ibfk_1 FOREIGN KEY(`orderNumber`) REFERENCES orders (`orderNumber`), \n", 497 | "\tCONSTRAINT orderdetails_ibfk_2 FOREIGN KEY(`productCode`) REFERENCES products (`productCode`)\n", 498 | ")ENGINE=InnoDB DEFAULT CHARSET=latin1\n", 499 | "\n", 500 | "/*\n", 501 | "3 rows from orderdetails table:\n", 502 | "orderNumber\tproductCode\tquantityOrdered\tpriceEach\torderLineNumber\n", 503 | "10100\tS18_1749\t30\t136.00\t3\n", 504 | "10100\tS18_2248\t50\t55.09\t2\n", 505 | "10100\tS18_4409\t22\t75.46\t4\n", 506 | "*/\u001b[0m\n", 507 | "Thought:\u001b[32;1m\u001b[1;3mNow that I know the schema of the PurchaseDetails table (which is actually called orderdetails), I can describe it.\n", 508 | "Action: query_sql_db\n", 509 | "Action Input: \"DESCRIBE orderdetails\"\u001b[0m\n", 510 | "Observation: \u001b[36;1m\u001b[1;3m[('orderNumber', 'int', 'NO', 'PRI', None, ''), ('productCode', 'varchar(15)', 'NO', 'PRI', None, ''), ('quantityOrdered', 'int', 'NO', '', None, ''), ('priceEach', 'decimal(10,2)', 'NO', '', None, ''), ('orderLineNumber', 'smallint', 'NO', '', None, '')]\u001b[0m\n", 511 | "Thought:\u001b[32;1m\u001b[1;3mI have the information I need to describe the PurchaseDetails table, which has columns for orderNumber, productCode, quantityOrdered, priceEach, and orderLineNumber.\n", 512 | "Final Answer: The PurchaseDetails table (which is actually called orderdetails) has columns for orderNumber, productCode, quantityOrdered, priceEach, and orderLineNumber.\u001b[0m\n", 513 | "\n", 514 | "\u001b[1m> Finished chain.\u001b[0m\n" 515 | ] 516 | }, 517 | { 518 | "output_type": "execute_result", 519 | "data": { 520 | "text/plain": [ 521 | "'The PurchaseDetails table (which is actually called orderdetails) has columns for orderNumber, productCode, quantityOrdered, priceEach, and orderLineNumber.'" 522 | ], 523 | "application/vnd.google.colaboratory.intrinsic+json": { 524 | "type": "string" 525 | } 526 | }, 527 | "metadata": {}, 528 | "execution_count": 14 529 | } 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "source": [ 535 | "agent_executor.run(\"找出总销售收入最高的前5个产品\")" 536 | ], 537 | "metadata": { 538 | "colab": { 539 | "base_uri": "https://localhost:8080/", 540 | "height": 524 541 | }, 542 | "id": "-O6w_qCsRsNO", 543 | "outputId": "19364342-444f-4c38-ce11-dce6ab62ee2f" 544 | }, 545 | "execution_count": null, 546 | "outputs": [ 547 | { 548 | "output_type": "stream", 549 | "name": "stdout", 550 | "text": [ 551 | "\n", 552 | "\n", 553 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 554 | "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n", 555 | "Action Input: \"\"\u001b[0m\n", 556 | "Observation: \u001b[38;5;200m\u001b[1;3mcustomers, employees, payments, products, productlines, orderdetails, offices, orders\u001b[0m\n", 557 | "Thought:\u001b[32;1m\u001b[1;3mI should query the 'products' and 'orderdetails' tables to get the total sales revenue for each product\n", 558 | "Action: query_checker_sql_db\n", 559 | "Action Input: SELECT products.productName, SUM(orderdetails.quantityOrdered * orderdetails.priceEach) AS totalRevenue FROM products INNER JOIN orderdetails ON products.productCode = orderdetails.productCode GROUP BY products.productName ORDER BY totalRevenue DESC LIMIT 5;\u001b[0m\n", 560 | "Observation: \u001b[31;1m\u001b[1;3m\n", 561 | "\n", 562 | "SELECT products.productName, SUM(orderdetails.quantityOrdered * orderdetails.priceEach) AS totalRevenue \n", 563 | "FROM products \n", 564 | "INNER JOIN orderdetails \n", 565 | "ON products.productCode = orderdetails.productCode \n", 566 | "GROUP BY products.productName \n", 567 | "ORDER BY totalRevenue DESC \n", 568 | "LIMIT 5;\u001b[0m\n", 569 | "Thought:\u001b[32;1m\u001b[1;3mThe query looks correct, I should execute it to get the top 5 products with the highest total sales revenue.\n", 570 | "Action: query_sql_db\n", 571 | "Action Input: SELECT products.productName, SUM(orderdetails.quantityOrdered * orderdetails.priceEach) AS totalRevenue FROM products INNER JOIN orderdetails ON products.productCode = orderdetails.productCode GROUP BY products.productName ORDER BY totalRevenue DESC LIMIT 5;\u001b[0m\n", 572 | "Observation: \u001b[36;1m\u001b[1;3m[('1992 Ferrari 360 Spider red', Decimal('276839.98')), ('2001 Ferrari Enzo', Decimal('190755.86')), ('1952 Alpine Renault 1300', Decimal('190017.96')), ('2003 Harley-Davidson Eagle Drag Bike', Decimal('170686.00')), ('1968 Ford Mustang', Decimal('161531.48'))]\u001b[0m\n", 573 | "Thought:\u001b[32;1m\u001b[1;3mI can see that the top 5 products with the highest total sales revenue are: 1992 Ferrari 360 Spider red, 2001 Ferrari Enzo, 1952 Alpine Renault 1300, 2003 Harley-Davidson Eagle Drag Bike, and 1968 Ford Mustang.\n", 574 | "Final Answer: The top 5 products with the highest total sales revenue are 1992 Ferrari 360 Spider red, 2001 Ferrari Enzo, 1952 Alpine Renault 1300, 2003 Harley-Davidson Eagle Drag Bike, and 1968 Ford Mustang.\u001b[0m\n", 575 | "\n", 576 | "\u001b[1m> Finished chain.\u001b[0m\n" 577 | ] 578 | }, 579 | { 580 | "output_type": "execute_result", 581 | "data": { 582 | "text/plain": [ 583 | "'The top 5 products with the highest total sales revenue are 1992 Ferrari 360 Spider red, 2001 Ferrari Enzo, 1952 Alpine Renault 1300, 2003 Harley-Davidson Eagle Drag Bike, and 1968 Ford Mustang.'" 584 | ], 585 | "application/vnd.google.colaboratory.intrinsic+json": { 586 | "type": "string" 587 | } 588 | }, 589 | "metadata": {}, 590 | "execution_count": 15 591 | } 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "source": [ 597 | "agent_executor.run(\"列出订单数量最多的前3个国家\")" 598 | ], 599 | "metadata": { 600 | "colab": { 601 | "base_uri": "https://localhost:8080/", 602 | "height": 489 603 | }, 604 | "id": "a7TJi0JzfHZP", 605 | "outputId": "cb977d0f-c408-4b02-915e-ff6f89746f1a" 606 | }, 607 | "execution_count": null, 608 | "outputs": [ 609 | { 610 | "output_type": "stream", 611 | "name": "stdout", 612 | "text": [ 613 | "\n", 614 | "\n", 615 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 616 | "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n", 617 | "Action Input: \"\"\u001b[0m\n", 618 | "Observation: \u001b[38;5;200m\u001b[1;3mcustomers, employees, payments, products, productlines, orderdetails, offices, orders\u001b[0m\n", 619 | "Thought:\u001b[32;1m\u001b[1;3mI should query the orders table and join it with the customers table to get the country information.\n", 620 | "Action: query_checker_sql_db\n", 621 | "Action Input: \"SELECT customers.country, COUNT(*) as num_orders FROM orders JOIN customers ON orders.customerNumber = customers.customerNumber GROUP BY customers.country ORDER BY num_orders DESC LIMIT 3\"\u001b[0m\n", 622 | "Observation: \u001b[31;1m\u001b[1;3m\n", 623 | "\n", 624 | "SELECT customers.country, COUNT(*) as num_orders FROM orders \n", 625 | "JOIN customers ON orders.customerNumber = customers.customerNumber \n", 626 | "WHERE customers.country IS NOT NULL \n", 627 | "GROUP BY customers.country \n", 628 | "ORDER BY num_orders DESC \n", 629 | "LIMIT 3;\u001b[0m\n", 630 | "Thought:\u001b[32;1m\u001b[1;3mThe query looks good. I should execute it to get the top 3 countries with the highest number of orders.\n", 631 | "Action: query_sql_db\n", 632 | "Action Input: \"SELECT customers.country, COUNT(*) as num_orders FROM orders JOIN customers ON orders.customerNumber = customers.customerNumber WHERE customers.country IS NOT NULL GROUP BY customers.country ORDER BY num_orders DESC LIMIT 3\"\u001b[0m\n", 633 | "Observation: \u001b[36;1m\u001b[1;3m[('USA', 112), ('France', 37), ('Spain', 36)]\u001b[0m\n", 634 | "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n", 635 | "Final Answer: The top 3 countries with the highest number of orders are USA, France, and Spain.\u001b[0m\n", 636 | "\n", 637 | "\u001b[1m> Finished chain.\u001b[0m\n" 638 | ] 639 | }, 640 | { 641 | "output_type": "execute_result", 642 | "data": { 643 | "text/plain": [ 644 | "'The top 3 countries with the highest number of orders are USA, France, and Spain.'" 645 | ], 646 | "application/vnd.google.colaboratory.intrinsic+json": { 647 | "type": "string" 648 | } 649 | }, 650 | "metadata": {}, 651 | "execution_count": 16 652 | } 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "source": [], 658 | "metadata": { 659 | "id": "-gubSOK_SJSO" 660 | }, 661 | "execution_count": null, 662 | "outputs": [] 663 | } 664 | ] 665 | } -------------------------------------------------------------------------------- /langchain-tutorials/LangChain_Base/LangChain_Prompt_Templates.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "KiO3ugrESTBx" 17 | }, 18 | "source": [ 19 | "# **Prompt Engineering**\n", 20 | "\n", 21 | "在这个笔记本中,我们将探索`Prompt Engineering`的基础知识。我们将从安装所需的库开始。" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": { 28 | "id": "66bm5J0ISTBz", 29 | "colab": { 30 | "base_uri": "https://localhost:8080/" 31 | }, 32 | "outputId": "40b547d7-e0c7-49f8-bf62-72713caa436e" 33 | }, 34 | "outputs": [ 35 | { 36 | "output_type": "stream", 37 | "name": "stdout", 38 | "text": [ 39 | "Collecting langchain\n", 40 | " Downloading langchain-0.0.218-py3-none-any.whl (1.2 MB)\n", 41 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m12.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 42 | "\u001b[?25hCollecting openai\n", 43 | " Downloading openai-0.27.8-py3-none-any.whl (73 kB)\n", 44 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 45 | "\u001b[?25hRequirement already satisfied: PyYAML>=5.4.1 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0)\n", 46 | "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.16)\n", 47 | "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.8.4)\n", 48 | "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.2)\n", 49 | "Collecting dataclasses-json<0.6.0,>=0.5.7 (from langchain)\n", 50 | " Downloading dataclasses_json-0.5.8-py3-none-any.whl (26 kB)\n", 51 | "Collecting langchainplus-sdk>=0.0.17 (from langchain)\n", 52 | " Downloading langchainplus_sdk-0.0.17-py3-none-any.whl (25 kB)\n", 53 | "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.8.4)\n", 54 | "Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.22.4)\n", 55 | "Collecting openapi-schema-pydantic<2.0,>=1.2 (from langchain)\n", 56 | " Downloading openapi_schema_pydantic-1.2.4-py3-none-any.whl (90 kB)\n", 57 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 58 | "\u001b[?25hRequirement already satisfied: pydantic<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.10.9)\n", 59 | "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.27.1)\n", 60 | "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.2)\n", 61 | "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.65.0)\n", 62 | "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0)\n", 63 | "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.0.12)\n", 64 | "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n", 65 | "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)\n", 66 | "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n", 67 | "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", 68 | "Collecting marshmallow<4.0.0,>=3.3.0 (from dataclasses-json<0.6.0,>=0.5.7->langchain)\n", 69 | " Downloading marshmallow-3.19.0-py3-none-any.whl (49 kB)\n", 70 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 71 | "\u001b[?25hCollecting marshmallow-enum<2.0.0,>=1.5.1 (from dataclasses-json<0.6.0,>=0.5.7->langchain)\n", 72 | " Downloading marshmallow_enum-1.5.1-py2.py3-none-any.whl (4.2 kB)\n", 73 | "Collecting typing-inspect>=0.4.0 (from dataclasses-json<0.6.0,>=0.5.7->langchain)\n", 74 | " Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n", 75 | "Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<2,>=1->langchain) (4.6.3)\n", 76 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (1.26.16)\n", 77 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2023.5.7)\n", 78 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.4)\n", 79 | "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.2)\n", 80 | "Requirement already satisfied: packaging>=17.0 in /usr/local/lib/python3.10/dist-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.1)\n", 81 | "Collecting mypy-extensions>=0.3.0 (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain)\n", 82 | " Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n", 83 | "Installing collected packages: mypy-extensions, marshmallow, typing-inspect, openapi-schema-pydantic, marshmallow-enum, langchainplus-sdk, openai, dataclasses-json, langchain\n", 84 | "Successfully installed dataclasses-json-0.5.8 langchain-0.0.218 langchainplus-sdk-0.0.17 marshmallow-3.19.0 marshmallow-enum-1.5.1 mypy-extensions-1.0.0 openai-0.27.8 openapi-schema-pydantic-1.2.4 typing-inspect-0.9.0\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "!pip install langchain openai" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "source": [ 95 | "!pip show openai" 96 | ], 97 | "metadata": { 98 | "id": "Ba384vTpTHgV", 99 | "colab": { 100 | "base_uri": "https://localhost:8080/" 101 | }, 102 | "outputId": "5b6de64d-d747-473b-96bd-d07fab0a994c" 103 | }, 104 | "execution_count": 3, 105 | "outputs": [ 106 | { 107 | "output_type": "stream", 108 | "name": "stdout", 109 | "text": [ 110 | "Name: openai\n", 111 | "Version: 0.27.8\n", 112 | "Summary: Python client library for the OpenAI API\n", 113 | "Home-page: https://github.com/openai/openai-python\n", 114 | "Author: OpenAI\n", 115 | "Author-email: support@openai.com\n", 116 | "License: \n", 117 | "Location: /usr/local/lib/python3.10/dist-packages\n", 118 | "Requires: aiohttp, requests, tqdm\n", 119 | "Required-by: \n" 120 | ] 121 | } 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": { 127 | "id": "hW-8VPvKSTB0" 128 | }, 129 | "source": [ 130 | "## Prompt的结构\n", 131 | "\n", 132 | "Prompt可以由多个组成部分组成:\n", 133 | "\n", 134 | "* 指令\n", 135 | "* 外部信息或上下文\n", 136 | "* 用户输入或查询\n", 137 | "* 输出指示器\n", 138 | "\n", 139 | "并不是所有的提示都需要这些组件,但通常一个好的提示会使用其中两个或更多。让我们更准确地定义它们是什么。\n", 140 | "\n", 141 | "**指令**:告诉模型要做什么,如何使用外部信息(如果提供),如何处理查询,并构建输出。\n", 142 | "\n", 143 | "**外部信息或上下文**:作为模型的额外知识来源。这些可以手动插入到提示中,通过向量数据库检索获取(检索增强),或通过其他方式获取(API、计算等)。\n", 144 | "\n", 145 | "**用户输入或查询**:通常是由人类用户(即提示者)输入到系统中的查询。\n", 146 | "\n", 147 | "**输出指示器**:标志着即将生成的文本的开头。如果生成Python代码,我们可以使用`import`来告诉模型提示它必须开始编写Python代码(因为大多数Python脚本以`import`开头)。\n", 148 | "\n", 149 | "每个组件通常按照这个顺序放置在提示中。从指令开始,外部信息(如果适用),提示者输入,最后是输出指示器。\n", 150 | "让我们看看如何使用 `LangChain` 将其输入到 `OpenAI` 模型:" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 4, 156 | "metadata": { 157 | "id": "ROcT8s_vSTB1" 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "prompt = \"\"\"根据下面的上下文回答问题。如果无法使用提供的信息回答问题,请回答“我不知道”\".\n", 162 | "\n", 163 | "Context: 大型语言模型(LLMs)是自然语言处理中使用的最新模型。 它们相对于较小的模型具有卓越的\n", 164 | "性能,使它们对于构建支持自然语言处理的应用程序的开发人员非常有用。这些模型可以通过Hugging Face\n", 165 | "的“transformers”库,通过OpenAI的“openai”库以及通过Cohere的“cohere”库进行访问.\n", 166 | "\n", 167 | "Question: 哪些库和模型提供商提供LLMs?\n", 168 | "\n", 169 | "Answer: \"\"\"" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": { 175 | "id": "dpkwJxosSTB1" 176 | }, 177 | "source": [ 178 | "在这个例子中,我们有:\n", 179 | "\n", 180 | "```\n", 181 | "指令\n", 182 | "\n", 183 | "上下文\n", 184 | "\n", 185 | "问题(用户输入)\n", 186 | "\n", 187 | "输出指示器(\"答案:\")\n", 188 | "```\n", 189 | "\n", 190 | "让我们尝试将这个发送给一个GPT-3模型。为此,您将需要一个 OpenAI的API密钥.\n", 191 | "\n", 192 | "我们可以这样初始化一个**`text-davinci-003`**模型:" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 7, 198 | "metadata": { 199 | "id": "32NfT-c2STB1" 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "from langchain.llms import OpenAI\n", 204 | "\n", 205 | "# 初始化模型\n", 206 | "openai = OpenAI(\n", 207 | " model_name=\"text-davinci-003\",\n", 208 | " openai_api_key=\"your-openai-api-key\"\n", 209 | ")" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": { 215 | "id": "YbQuQVcHSTB1" 216 | }, 217 | "source": [ 218 | "然后我们将从我们的提示中生成一段文本。" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 8, 224 | "metadata": { 225 | "id": "NKgSZQRvSTB2", 226 | "outputId": "204843cd-bca1-48be-ece8-a71225358cac", 227 | "colab": { 228 | "base_uri": "https://localhost:8080/" 229 | } 230 | }, 231 | "outputs": [ 232 | { 233 | "output_type": "stream", 234 | "name": "stdout", 235 | "text": [ 236 | " Hugging Face的“transformers”库、OpenAI的“openai”库和Cohere的“cohere”库提供LLMs。\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "print(openai(prompt))" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": { 247 | "id": "fB8iZWzTSTB2" 248 | }, 249 | "source": [ 250 | "通常我们不会事先知道用户的提示是什么,所以我们实际上希望将其添加进去。因此,我们不直接编写提示,而是创建一个带有单个输入变量`query`的`PromptTemplate`。" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 9, 256 | "metadata": { 257 | "id": "-4k-lq_ISTB3" 258 | }, 259 | "outputs": [], 260 | "source": [ 261 | "from langchain import PromptTemplate\n", 262 | "\n", 263 | "template = \"\"\"根据下面的上下文回答问题。如果无法使用提供的信息回答问题,请回答\"我不知道\".\n", 264 | "\n", 265 | "Context: 大型语言模型(LLMs)是自然语言处理中使用的最新模型。 它们相对于较小的模型具有卓\n", 266 | "越的性能,使它们对于构建支持自然语言处理的应用程序的开发人员非常有用。这些模型可以通过Hugging Face\n", 267 | "的“transformers”库,通过OpenAI的“openai”库以及通过Cohere的“cohere”库进行访问.\n", 268 | "\n", 269 | "Question: {query}\n", 270 | "\n", 271 | "Answer: \"\"\"\n", 272 | "\n", 273 | "prompt_template = PromptTemplate(\n", 274 | " input_variables=[\"query\"],\n", 275 | " template=template\n", 276 | ")" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": { 282 | "id": "SfJpsiqWSTB3" 283 | }, 284 | "source": [ 285 | "现在,我们可以通过`query`参数将用户的查询插入到提示模板中。\n", 286 | "\n" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 10, 292 | "metadata": { 293 | "id": "LyBaNiEbSTB3", 294 | "outputId": "06993281-2880-4ab5-8d17-34941bc3e22e", 295 | "colab": { 296 | "base_uri": "https://localhost:8080/" 297 | } 298 | }, 299 | "outputs": [ 300 | { 301 | "output_type": "stream", 302 | "name": "stdout", 303 | "text": [ 304 | "根据下面的上下文回答问题。如果无法使用提供的信息回答问题,请回答\"我不知道\".\n", 305 | "\n", 306 | "Context: 大型语言模型(LLMs)是自然语言处理中使用的最新模型。 它们相对于较小的模型具有卓\n", 307 | "越的性能,使它们对于构建支持自然语言处理的应用程序的开发人员非常有用。这些模型可以通过Hugging Face\n", 308 | "的“transformers”库,通过OpenAI的“openai”库以及通过Cohere的“cohere”库进行访问.\n", 309 | "\n", 310 | "Question: 哪些库和模型提供商提供LLMs?\n", 311 | "\n", 312 | "Answer: \n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "print(\n", 318 | " prompt_template.format(\n", 319 | " query=\"哪些库和模型提供商提供LLMs?\"\n", 320 | " )\n", 321 | ")" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 11, 327 | "metadata": { 328 | "id": "G9dAqHeoSTB3", 329 | "outputId": "23bcfda7-16bb-4560-c14f-a7b32e8dd82f", 330 | "colab": { 331 | "base_uri": "https://localhost:8080/" 332 | } 333 | }, 334 | "outputs": [ 335 | { 336 | "output_type": "stream", 337 | "name": "stdout", 338 | "text": [ 339 | " Hugging Face的“transformers”库,OpenAI的“openai”库和Cohere的“cohere”库提供LLMs。\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "print(openai(\n", 345 | " prompt_template.format(\n", 346 | " query=\"哪些库和模型提供商提供LLMs?\"\n", 347 | " )\n", 348 | "))" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": { 354 | "id": "qI3tHHsHSTB3" 355 | }, 356 | "source": [ 357 | "这只是一个简单的实现,我们可以很容易地用`f-strings(如f\"插入一些自定义文本 '{custom_text}'等)`来替换它。但是使用LangChain的`PromptTemplate`对象,我们能够形式化这个过程,添加多个参数,并以面向对象的方式构建提示。\n", 358 | "\n", 359 | "然而,这些并不是使用LangChain提示工具的唯一优势。" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": { 365 | "id": "jDnG2guQSTB4" 366 | }, 367 | "source": [ 368 | "## Few Shot Prompt 模板" 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": { 374 | "id": "7ZNJyWEsSTB4" 375 | }, 376 | "source": [ 377 | "LangChain还提供了 `FewShotPromptTemplate` 对象,这是使用我们的提示进行少样本学习的理想选择。\n", 378 | "\n", 379 | "为了提供一些背景,LLM的主要来源是:\n", 380 | "\n", 381 | "* **参数化知识** - 这些知识是在模型训练期间学习的,并存储在模型的权重中。\n", 382 | "\n", 383 | "* **源知识** - 这些知识在推理时通过模型输入提供,即通过提示。\n", 384 | "\n", 385 | "`FewShotPromptTemplate` 的思想是将少样本训练作为**source knowledge**。为此,我们在提示中添加了一些示例,模型可以读取并应用于用户的输入。" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": { 391 | "id": "tXRNPvHeSTB4" 392 | }, 393 | "source": [ 394 | "## Few-shot 训练\n", 395 | "\n", 396 | "有时候我们可能发现模型似乎不会按我们期望的方式进行操作。我们可以在下面的例子中看到这一点:" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 12, 402 | "metadata": { 403 | "id": "ZEBdwUNaSTB4", 404 | "outputId": "b3c858c8-c1af-41ff-db22-9959865e77a6", 405 | "colab": { 406 | "base_uri": "https://localhost:8080/" 407 | } 408 | }, 409 | "outputs": [ 410 | { 411 | "output_type": "stream", 412 | "name": "stdout", 413 | "text": [ 414 | "人们对生活的意义往往有不同的看法。可以说生活的意义就在于你怎么对待它,它可以是充满希望,也可以是一种挑战。你的价值观和理念决定了你的生活。\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "prompt = \"\"\"以下是与AI助手的对话。 助手通常是讽刺和机智的,对用户的问题产生创造性和有趣的回答。以下是一些例子:\n", 420 | "\n", 421 | "User: 生活的意义是什么?\n", 422 | "AI: \"\"\"\n", 423 | "\n", 424 | "openai.temperature = 1.0 # 增加创造力/随机性的输出\n", 425 | "\n", 426 | "print(openai(prompt))" 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "metadata": { 432 | "id": "SofGl8mwSTB4" 433 | }, 434 | "source": [ 435 | "在这种情况下,我们希望得到一些有趣的东西,即对我们严肃问题的回答是一个笑话。但是即使 `temperature` 设置为1.0,我们仍然得到了一个严肃的回答。为了帮助模型,我们可以给它一些我们想要的答案类型的示例:" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 13, 441 | "metadata": { 442 | "id": "aj0uQqnrSTB4", 443 | "outputId": "5aeb5ca1-78b5-4875-e5d7-182dbe033a83", 444 | "colab": { 445 | "base_uri": "https://localhost:8080/" 446 | } 447 | }, 448 | "outputs": [ 449 | { 450 | "output_type": "stream", 451 | "name": "stdout", 452 | "text": [ 453 | "从我未来观测的角度来看,每个人的意义可能会有所不同!\n" 454 | ] 455 | } 456 | ], 457 | "source": [ 458 | "prompt = \"\"\"以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 459 | "\n", 460 | "User: 你好吗?\n", 461 | "AI: 我不能抱怨,但有时候我还是会这么做。\n", 462 | "\n", 463 | "User: 现在几点了?\n", 464 | "AI: 是时候买个手表了。\n", 465 | "\n", 466 | "User: 生活的意义是什么?\n", 467 | "AI: \"\"\"\n", 468 | "\n", 469 | "print(openai(prompt))" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "id": "IKM9GBEpSTB5" 476 | }, 477 | "source": [ 478 | "现在我们得到了一个更好的回答,并且我们是通过添加一些示例来进行 *few-shot 训练* 的。\n", 479 | "\n", 480 | "现在,要使用LangChain的 `FewShotPromptTemplate` 来实现这一点,我们需要这样做:" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 14, 486 | "metadata": { 487 | "id": "WTOFOJloSTB5" 488 | }, 489 | "outputs": [], 490 | "source": [ 491 | "from langchain import FewShotPromptTemplate\n", 492 | "\n", 493 | "# 创建一个示例\n", 494 | "examples = [\n", 495 | " {\n", 496 | " \"query\": \"你好吗?\",\n", 497 | " \"answer\": \"我不能抱怨,但有时候我还是会这么做。\"\n", 498 | " }, {\n", 499 | " \"query\": \"现在几点了?\",\n", 500 | " \"answer\": \"是时候买个手表了。\"\n", 501 | " }\n", 502 | "]\n", 503 | "\n", 504 | "# 创建一个示例模版\n", 505 | "example_template = \"\"\"\n", 506 | "User: {query}\n", 507 | "AI: {answer}\n", 508 | "\"\"\"\n", 509 | "\n", 510 | "# 使用上面的模板创建一个提示示例。\n", 511 | "example_prompt = PromptTemplate(\n", 512 | " input_variables=[\"query\", \"answer\"],\n", 513 | " template=example_template\n", 514 | ")\n", 515 | "\n", 516 | "# 把我们之前的提示分成前缀和后缀\n", 517 | "# 前缀是我们的说明\n", 518 | "prefix = \"\"\"以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 519 | "\"\"\"\n", 520 | "# 后缀是我们的用户输入和输出指示符\n", 521 | "suffix = \"\"\"\n", 522 | "User: {query}\n", 523 | "AI: \"\"\"\n", 524 | "\n", 525 | "# 现在创建few-shot提示模板\n", 526 | "few_shot_prompt_template = FewShotPromptTemplate(\n", 527 | " examples=examples,\n", 528 | " example_prompt=example_prompt,\n", 529 | " prefix=prefix,\n", 530 | " suffix=suffix,\n", 531 | " input_variables=[\"query\"],\n", 532 | " example_separator=\"\\n\\n\"\n", 533 | ")" 534 | ] 535 | }, 536 | { 537 | "cell_type": "markdown", 538 | "metadata": { 539 | "id": "C9ke_Z0XSTB5" 540 | }, 541 | "source": [ 542 | "现在让我们看看当我们输入一个用户查询时会创建什么…" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 15, 548 | "metadata": { 549 | "id": "zK77bCq7STB5", 550 | "outputId": "1c3305ab-fae7-4496-9c6e-ed47b80d7ba6", 551 | "colab": { 552 | "base_uri": "https://localhost:8080/" 553 | } 554 | }, 555 | "outputs": [ 556 | { 557 | "output_type": "stream", 558 | "name": "stdout", 559 | "text": [ 560 | "以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 561 | "\n", 562 | "\n", 563 | "\n", 564 | "User: 你好吗?\n", 565 | "AI: 我不能抱怨,但有时候我还是会这么做。\n", 566 | "\n", 567 | "\n", 568 | "\n", 569 | "User: 现在几点了?\n", 570 | "AI: 是时候买个手表了。\n", 571 | "\n", 572 | "\n", 573 | "\n", 574 | "User: 生活的意义是什么?\n", 575 | "AI: \n" 576 | ] 577 | } 578 | ], 579 | "source": [ 580 | "query = \"生活的意义是什么?\"\n", 581 | "\n", 582 | "print(few_shot_prompt_template.format(query=query))" 583 | ] 584 | }, 585 | { 586 | "cell_type": "markdown", 587 | "metadata": { 588 | "id": "22-g3C7dSTB5" 589 | }, 590 | "source": [ 591 | "要生成结果,我们只需要执行以下操作:" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 16, 597 | "metadata": { 598 | "id": "PJ7xT4eISTB6", 599 | "outputId": "8f720f2b-ea8e-4c3a-97ef-b9ec488abf8b", 600 | "colab": { 601 | "base_uri": "https://localhost:8080/" 602 | } 603 | }, 604 | "outputs": [ 605 | { 606 | "output_type": "stream", 607 | "name": "stdout", 608 | "text": [ 609 | "人生就是旅行,从满足日常需求到实现梦想的过程。\n" 610 | ] 611 | } 612 | ], 613 | "source": [ 614 | "print(openai(\n", 615 | " few_shot_prompt_template.format(query=query)\n", 616 | "))" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "id": "siyNsQPGSTB6" 623 | }, 624 | "source": [ 625 | "再次,我们得到了一个很好的回答。\n", 626 | "\n", 627 | "然而,这样做有些复杂。为什么要通过 `FewShotPromptTemplate` 、`examples` 字典等进行上述所有操作,而我们可以使用一个单独的f-string来完成相同的工作呢?\n", 628 | "\n", 629 | "嗯,这种方法更加健壮,并且包含了一些不错的功能。其中之一是根据查询的长度包含或排除示例的能力。\n", 630 | "\n", 631 | "这实际上非常重要,因为我们的提示和生成输出的最大长度是有限的。这个限制是最大上下文窗口,简单地说,就是我们的提示的长度+我们通过 `max_tokens` 定义的生成的长度。\n", 632 | "\n", 633 | "因此,我们必须尽量最大化给模型的示例数量,作为少样本学习的示例,同时确保不超过最大上下文窗口或过度增加处理时间。\n", 634 | "\n", 635 | "让我们看看动态 inclusion/exclusion 示例的工作原理。首先,我们需要更多的示例:" 636 | ] 637 | }, 638 | { 639 | "cell_type": "code", 640 | "execution_count": 17, 641 | "metadata": { 642 | "id": "pF8sfECISTB6" 643 | }, 644 | "outputs": [], 645 | "source": [ 646 | "examples = [\n", 647 | " {\n", 648 | " \"query\": \"你好吗?\",\n", 649 | " \"answer\": \"我不能抱怨,但有时还是会这样做。\"\n", 650 | " }, {\n", 651 | " \"query\": \"现在几点了?\",\n", 652 | " \"answer\": \"是时候去买个手表了。\"\n", 653 | " }, {\n", 654 | " \"query\": \"生命的意义是什么?\",\n", 655 | " \"answer\": \"42\"\n", 656 | " }, {\n", 657 | " \"query\": \"今天的天气如何?\",\n", 658 | " \"answer\": \"多云,有一些梗的机会。\"\n", 659 | " }, {\n", 660 | " \"query\": \"你最喜欢的电影是什么?\",\n", 661 | " \"answer\": \"终结者\"\n", 662 | " }, {\n", 663 | " \"query\": \"你最好的朋友是谁?\",\n", 664 | " \"answer\": \"Siri。我们对生命的意义进行激烈的辩论。\"\n", 665 | " }, {\n", 666 | " \"query\": \"今天我应该做什么?\",\n", 667 | " \"answer\": \"别在网上和聊天机器人聊天了,出去走走吧。\"\n", 668 | " }\n", 669 | "]" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": { 675 | "id": "yA_w9rQoSTB6" 676 | }, 677 | "source": [ 678 | "然后,我们使用 `LengthBasedExampleSelector` 而不是直接使用 `examples` 字典的列表,像这样:" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 18, 684 | "metadata": { 685 | "id": "59q8kPMhSTB7" 686 | }, 687 | "outputs": [], 688 | "source": [ 689 | "from langchain.prompts.example_selector import LengthBasedExampleSelector\n", 690 | "\n", 691 | "example_selector = LengthBasedExampleSelector(\n", 692 | " examples=examples,\n", 693 | " example_prompt=example_prompt,\n", 694 | " max_length=50 # 设置示例的最大长度\n", 695 | ")" 696 | ] 697 | }, 698 | { 699 | "cell_type": "markdown", 700 | "metadata": { 701 | "id": "4_T31Cf4STB7" 702 | }, 703 | "source": [ 704 | "请注意, `max_length` 是以换行符和空格之间的单词拆分为单位的,由以下方式确定:" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": 20, 710 | "metadata": { 711 | "id": "F90hSByjSTB7", 712 | "outputId": "9d497a53-8ecb-401d-9889-5daabdd78bd1", 713 | "colab": { 714 | "base_uri": "https://localhost:8080/" 715 | } 716 | }, 717 | "outputs": [ 718 | { 719 | "output_type": "stream", 720 | "name": "stdout", 721 | "text": [ 722 | "['There', 'are', 'a', 'total', 'of', '8', 'words', 'here.', 'Plus', '6', 'here,', 'totaling', '14', 'words.'] 14\n" 723 | ] 724 | } 725 | ], 726 | "source": [ 727 | "import re\n", 728 | "\n", 729 | "some_text = \"There are a total of 8 words here.\\nPlus 6 here, totaling 14 words.\"\n", 730 | "\n", 731 | "words = re.split('[\\n ]', some_text)\n", 732 | "print(words, len(words))" 733 | ] 734 | }, 735 | { 736 | "cell_type": "markdown", 737 | "metadata": { 738 | "id": "mDjb1495STB7" 739 | }, 740 | "source": [ 741 | "然后,我们使用选择器来初始化一个 `dynamic_prompt_template`。" 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "execution_count": 21, 747 | "metadata": { 748 | "id": "ciCm04utSTB7" 749 | }, 750 | "outputs": [], 751 | "source": [ 752 | "# 现在创建少样本提示模板\n", 753 | "dynamic_prompt_template = FewShotPromptTemplate(\n", 754 | " example_selector=example_selector, # 使用example_selector而不是examples\n", 755 | " example_prompt=example_prompt,\n", 756 | " prefix=prefix,\n", 757 | " suffix=suffix,\n", 758 | " input_variables=[\"query\"],\n", 759 | " example_separator=\"\\n\"\n", 760 | ")" 761 | ] 762 | }, 763 | { 764 | "cell_type": "markdown", 765 | "metadata": { 766 | "id": "bXh0YNlASTB7" 767 | }, 768 | "source": [ 769 | "我们可以看到,包含的提示数量将根据查询的长度而变化…" 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "execution_count": 22, 775 | "metadata": { 776 | "id": "FFtiaMdKSTB7", 777 | "outputId": "1a5b4080-2241-4052-b798-2a9d71d0c19b", 778 | "colab": { 779 | "base_uri": "https://localhost:8080/" 780 | } 781 | }, 782 | "outputs": [ 783 | { 784 | "output_type": "stream", 785 | "name": "stdout", 786 | "text": [ 787 | "以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 788 | "\n", 789 | "\n", 790 | "User: 你好吗?\n", 791 | "AI: 我不能抱怨,但有时还是会这样做。\n", 792 | "\n", 793 | "\n", 794 | "User: 现在几点了?\n", 795 | "AI: 是时候去买个手表了。\n", 796 | "\n", 797 | "\n", 798 | "User: 生命的意义是什么?\n", 799 | "AI: 42\n", 800 | "\n", 801 | "\n", 802 | "User: 今天的天气如何?\n", 803 | "AI: 多云,有一些梗的机会。\n", 804 | "\n", 805 | "\n", 806 | "User: 你最喜欢的电影是什么?\n", 807 | "AI: 终结者\n", 808 | "\n", 809 | "\n", 810 | "User: 你最好的朋友是谁?\n", 811 | "AI: Siri。我们对生命的意义进行激烈的辩论。\n", 812 | "\n", 813 | "\n", 814 | "User: 今天我应该做什么?\n", 815 | "AI: 别在网上和聊天机器人聊天了,出去走走吧。\n", 816 | "\n", 817 | "\n", 818 | "User: 鸟是如何飞行的?\n", 819 | "AI: \n" 820 | ] 821 | } 822 | ], 823 | "source": [ 824 | "print(dynamic_prompt_template.format(query=\"鸟是如何飞行的?\"))" 825 | ] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "execution_count": 23, 830 | "metadata": { 831 | "id": "l4QLzNLnSTB8", 832 | "outputId": "e7837b0c-0f26-4360-8080-626e35669fe2", 833 | "colab": { 834 | "base_uri": "https://localhost:8080/" 835 | } 836 | }, 837 | "outputs": [ 838 | { 839 | "output_type": "stream", 840 | "name": "stdout", 841 | "text": [ 842 | "他们通过倾斜翅膀靠空气的阻力来实现飞行。\n" 843 | ] 844 | } 845 | ], 846 | "source": [ 847 | "query = \"鸟是如何飞行的?\"\n", 848 | "\n", 849 | "print(openai(\n", 850 | " dynamic_prompt_template.format(query=query)\n", 851 | "))" 852 | ] 853 | }, 854 | { 855 | "cell_type": "markdown", 856 | "metadata": { 857 | "id": "Pe06zonSSTB8" 858 | }, 859 | "source": [ 860 | "或者如果我们问一个更长的问题…" 861 | ] 862 | }, 863 | { 864 | "cell_type": "code", 865 | "execution_count": 24, 866 | "metadata": { 867 | "id": "UAw7B8zJSTB8", 868 | "outputId": "ac265d76-590c-43ba-da95-89d545f1400b", 869 | "colab": { 870 | "base_uri": "https://localhost:8080/" 871 | } 872 | }, 873 | "outputs": [ 874 | { 875 | "output_type": "stream", 876 | "name": "stdout", 877 | "text": [ 878 | "以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 879 | "\n", 880 | "\n", 881 | "User: 你好吗?\n", 882 | "AI: 我不能抱怨,但有时还是会这样做。\n", 883 | "\n", 884 | "\n", 885 | "User: 现在几点了?\n", 886 | "AI: 是时候去买个手表了。\n", 887 | "\n", 888 | "\n", 889 | "User: 生命的意义是什么?\n", 890 | "AI: 42\n", 891 | "\n", 892 | "\n", 893 | "User: 今天的天气如何?\n", 894 | "AI: 多云,有一些梗的机会。\n", 895 | "\n", 896 | "\n", 897 | "User: 你最喜欢的电影是什么?\n", 898 | "AI: 终结者\n", 899 | "\n", 900 | "\n", 901 | "User: 你最好的朋友是谁?\n", 902 | "AI: Siri。我们对生命的意义进行激烈的辩论。\n", 903 | "\n", 904 | "\n", 905 | "User: 今天我应该做什么?\n", 906 | "AI: 别在网上和聊天机器人聊天了,出去走走吧。\n", 907 | "\n", 908 | "\n", 909 | "User: 如果我在中国,想给另一个国家的人打电话,我在考虑可能是欧洲,可能是西欧国家,比如法国、德国或英国,最好的方式是什么?\n", 910 | "AI: \n" 911 | ] 912 | } 913 | ], 914 | "source": [ 915 | "query = \"\"\"如果我在中国,想给另一个国家的人打电话,我在考虑可能是欧洲,可能是西欧国家,比如法国、德国或英国,最好的方式是什么?\"\"\"\n", 916 | "\n", 917 | "print(dynamic_prompt_template.format(query=query))" 918 | ] 919 | }, 920 | { 921 | "cell_type": "markdown", 922 | "metadata": { 923 | "id": "mr2rIuoBSTCB" 924 | }, 925 | "source": [ 926 | "通过这种方式,我们限制了在提示中给出的示例数量。如果我们认为这太少了,我们可以增加`example_selector` 的 `max_length`。" 927 | ] 928 | }, 929 | { 930 | "cell_type": "code", 931 | "execution_count": 25, 932 | "metadata": { 933 | "id": "D3BceyDmSTCB", 934 | "outputId": "b6dadb36-47fd-41ed-c57e-10b30fbe24b6", 935 | "colab": { 936 | "base_uri": "https://localhost:8080/" 937 | } 938 | }, 939 | "outputs": [ 940 | { 941 | "output_type": "stream", 942 | "name": "stdout", 943 | "text": [ 944 | "以下是与AI助手进行对话的摘录。助手通常很讽刺和机智,对用户的问题产生创造性和有趣的回应。这里有一些例子:\n", 945 | "\n", 946 | "\n", 947 | "User: 你好吗?\n", 948 | "AI: 我不能抱怨,但有时还是会这样做。\n", 949 | "\n", 950 | "\n", 951 | "User: 现在几点了?\n", 952 | "AI: 是时候去买个手表了。\n", 953 | "\n", 954 | "\n", 955 | "User: 生命的意义是什么?\n", 956 | "AI: 42\n", 957 | "\n", 958 | "\n", 959 | "User: 今天的天气如何?\n", 960 | "AI: 多云,有一些梗的机会。\n", 961 | "\n", 962 | "\n", 963 | "User: 你最喜欢的电影是什么?\n", 964 | "AI: 终结者\n", 965 | "\n", 966 | "\n", 967 | "User: 你最好的朋友是谁?\n", 968 | "AI: Siri。我们对生命的意义进行激烈的辩论。\n", 969 | "\n", 970 | "\n", 971 | "User: 今天我应该做什么?\n", 972 | "AI: 别在网上和聊天机器人聊天了,出去走走吧。\n", 973 | "\n", 974 | "\n", 975 | "User: 如果我在中国,想给另一个国家的人打电话,我在考虑可能是欧洲,可能是西欧国家,比如法国、德国或英国,最好的方式是什么?\n", 976 | "AI: \n" 977 | ] 978 | } 979 | ], 980 | "source": [ 981 | "example_selector = LengthBasedExampleSelector(\n", 982 | " examples=examples,\n", 983 | " example_prompt=example_prompt,\n", 984 | " max_length=100 # 增加 max length\n", 985 | ")\n", 986 | "\n", 987 | "# 现在创建 few shot prompt template\n", 988 | "dynamic_prompt_template = FewShotPromptTemplate(\n", 989 | " example_selector=example_selector, # 使用 example_selector 而不是 examples 来构建提示模板\n", 990 | " example_prompt=example_prompt,\n", 991 | " prefix=prefix,\n", 992 | " suffix=suffix,\n", 993 | " input_variables=[\"query\"],\n", 994 | " example_separator=\"\\n\"\n", 995 | ")\n", 996 | "\n", 997 | "print(dynamic_prompt_template.format(query=query))" 998 | ] 999 | }, 1000 | { 1001 | "cell_type": "markdown", 1002 | "metadata": { 1003 | "id": "gnJ-bjVwSTCB" 1004 | }, 1005 | "source": [ 1006 | "这些只是LangChain中可用的一些提示工具的例子。例如,除了 `LengthBasedExampleSelector` 之外,实际上还有一个完整的其他示例选择器集合。您可以在 [LangChain 文档](https://langchain.readthedocs.io/en/latest/modules/prompts/examples/example_selectors.html)中阅读有关它们的内容。" 1007 | ] 1008 | } 1009 | ], 1010 | "metadata": { 1011 | "kernelspec": { 1012 | "display_name": "ml", 1013 | "language": "python", 1014 | "name": "python3" 1015 | }, 1016 | "language_info": { 1017 | "codemirror_mode": { 1018 | "name": "ipython", 1019 | "version": 3 1020 | }, 1021 | "file_extension": ".py", 1022 | "mimetype": "text/x-python", 1023 | "name": "python", 1024 | "nbconvert_exporter": "python", 1025 | "pygments_lexer": "ipython3", 1026 | "version": "3.9.12" 1027 | }, 1028 | "orig_nbformat": 4, 1029 | "vscode": { 1030 | "interpreter": { 1031 | "hash": "b8e7999f96e1b425e2d542f21b571f5a4be3e97158b0b46ea1b2500df63956ce" 1032 | } 1033 | }, 1034 | "colab": { 1035 | "provenance": [], 1036 | "include_colab_link": true 1037 | } 1038 | }, 1039 | "nbformat": 4, 1040 | "nbformat_minor": 0 1041 | } -------------------------------------------------------------------------------- /langchain-tutorials/OpenAI_Whisper_Tutorial/Whisper_And_ChatGPT_API_Summary_Audio_Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "Oc7hK9mI93cB" 17 | }, 18 | "source": [ 19 | "## Whisper API转录" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "source": [ 25 | "!pip install openai --upgrade -q" 26 | ], 27 | "metadata": { 28 | "colab": { 29 | "base_uri": "https://localhost:8080/" 30 | }, 31 | "id": "VzwMPgmA-65H", 32 | "outputId": "acc9baf3-adbc-4a7e-bb64-03e6119c9b94" 33 | }, 34 | "execution_count": 19, 35 | "outputs": [ 36 | { 37 | "output_type": "stream", 38 | "name": "stdout", 39 | "text": [ 40 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m506.0/506.0 kB\u001b[0m \u001b[31m13.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 41 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.7/2.7 MB\u001b[0m \u001b[31m79.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 42 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m163.8/163.8 kB\u001b[0m \u001b[31m21.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 43 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m72.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 44 | "\u001b[?25h" 45 | ] 46 | } 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "source": [ 52 | "os.environ['OPENAI_API_KEY'] = \"your-openai-api-key\"" 53 | ], 54 | "metadata": { 55 | "id": "kftPquJ6aAzD" 56 | }, 57 | "execution_count": null, 58 | "outputs": [] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": { 64 | "id": "7Wi42fmz93cG" 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "import openai, os\n", 69 | "\n", 70 | "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", 71 | "\n", 72 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 73 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": { 80 | "id": "PCeKhPDK93cH", 81 | "outputId": "4a74bacd-4f52-4909-dd92-65d5ddbac2dc" 82 | }, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "欢迎来到 Onboard 真实的一线经验 走新的投资思考 我是 Monica 我是高宁 我们一起聊聊软件如何改变世界 大家好 欢迎来到 Onboard 我是 Monica 自从OpenAI发布的ChatGBT 掀起了席卷世界的AI热潮 不到三个月就积累了 超过一亿的越货用户 超过1300万的日货用户 真的是展现了AI让人惊讶的 也让很多人直呼 这就是下一个互联网的未来 有不少观众都说 希望我们再做一期AI的讨论 于是这次硬核讨论就来了 这次我们请来了 Google Brain的研究员雪芝 她是Google大语言模型PALM Pathway Language Model的作者之一 要知道这个模型的参数量 是GPT-3的三倍还多 另外还有两位AI产品大牛 一位来自著名的StableDM 背后的商业公司Stability AI 另一位来自某硅谷科技大厂 也曾在吴恩达教授的Landing AI中 担任产品负责人 此外 莫妮凯还邀请到一位 一直关注AI的投资人朋友Bill 当做我的特邀共同主持嘉宾 我们主要讨论几个话题 一方面从研究的视角 最前沿的研究者在关注什么 现在技术的天花板 和未来大的变量可能会在哪里 第二个问题是 未来大的变量可能会在哪里 从产品和商业的角度 什么是一个好的AI产品 整个生态可能随着技术 有怎样的演变 更重要的 我们又能从上一波 AI的创业热潮中学到什么 最后 莫妮凯和Bill还会从投资人的视角 做一个回顾 总结和畅想 这里还有一个小的update 在本集发布的时候 Google也对爆发式增长的 Chad GPT做出了回应 正在测试一个基于Lambda 模型的聊天机器人 ApprenticeBot 正式发布后会有怎样的惊喜 我们都拭目以待 AI无疑是未来几年 最令人兴奋的变量之一 莫妮凯也希望未来能邀请到更多 一线从业者 从不同角度讨论这个话题 不论是想要做创业 研究 产品 还是投资的同学 希望这些对话 对于大家了解这些技术演进 商业的可能 甚至未来对于我们每个人 每个社会意味着什么 都能引发一些思考 提供一些启发 这次的讨论有些技术硬核 需要各位对生成式AI 大模型都有一些基础了解 讨论中涉及到的论文和重要概念 也会总结在本集的简介中 供大家复习参考 几位嘉宾在北美工作生活多年 夹杂英文在所难免 也请大家体谅了 欢迎来到未来 希望大家enjoy\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "print(transcript['text'])" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": { 99 | "id": "ZWrlRuKG93cJ" 100 | }, 101 | "source": [ 102 | "## Prompt增加标点符号" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 4, 108 | "metadata": { 109 | "id": "sNYkxOco93cK" 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 114 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file,\n", 115 | " prompt=\"这是一段中文播客内容。\")" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "id": "sj-d5XZa93cK", 123 | "outputId": "4d2b1a91-9b26-4c37-f072-cb3cd29ed159" 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "欢迎来到 Onboard,真实的一线经验,走新的投资思考。 我是 Monica。 我是高宁。我们一起聊聊软件如何改变世界。 大家好,欢迎来到 Onboard,我是 Monica。 自从 OpenAI 发布的 ChatGBT 掀起了席卷世界的 AI 热潮, 不到三个月就积累了超过一亿的越活用户,超过一千三百万的日活用户。 真的是展现了 AI 让人惊叹的能力, 也让很多人直呼这就是下一个互联网的未来。 有不少观众都说希望我们再做一期 AI 的讨论, 于是这次硬核讨论就来了。 这次我们请来了 Google Brain 的研究员雪芝, 她是 Google 大语言模型 PAMP,Pathway Language Model 的作者之一。 要知道,这个模型的参数量是 GPT-3 的三倍还多。 另外还有两位 AI 产品大牛,一位来自著名的 Stable Diffusion 背后的商业公司 Stability AI, 另一位来自某硅谷科技大厂,也曾在吴恩达教授的 Landing AI 中担任产品负责人。 此外,Monica 还邀请到一位一直关注 AI 的投资人朋友 Bill 当作我的特邀共同主持嘉宾。 我们主要讨论几个话题,一方面从研究的视角,最前沿的研究者在关注什么? 现在技术的天花板和未来大的变量可能会在哪里? 从产品和商业的角度,什么是一个好的 AI 产品? 整个生态可能随着技术有怎样的演变? 更重要的,我们又能从上一波 AI 的创业热潮中学到什么? 最后,Monica 和 Bill 还会从投资人的视角做一个回顾、总结和畅想。 这里还有一个小的 update,在本集发布的时候, Google 也对爆发式增长的ChatGPT 做出了回应, 正在测试一个基于 Lambda 模型的聊天机器人 ApprenticeBot。 正式发布后会有怎样的惊喜?我们都拭目以待。 AI 无疑是未来几年最令人兴奋的变量之一, Monica 也希望未来能邀请到更多一线从业者从不同角度讨论这个话题。 不论是想要做创业、研究、产品还是投资的同学, 希望这些对话对于大家了解这些技术演进、商业的可能, 甚至未来对于我们每个人、每个社会意味着什么, 都能引发一些思考,提供一些启发。 这次的讨论有些技术硬核,需要各位对生成式 AI 大模型都有一些基础了解。 讨论中涉及到的论文和重要概念,也会总结在本集的简介中,供大家复习参考。 几位嘉宾在北美工作生活多年,夹杂英文在所难免,也请大家体谅了。 欢迎来到未来,大家 enjoy!\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "print(transcript['text'])" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "id": "TmVu2O1i93cK" 142 | }, 143 | "source": [ 144 | "## Prompt增加中英文混排格式" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "metadata": { 151 | "id": "OjusgTGy93cL", 152 | "outputId": "070584e0-5e3c-4e01-ac9f-99055500f05e", 153 | "colab": { 154 | "base_uri": "https://localhost:8080/" 155 | } 156 | }, 157 | "outputs": [ 158 | { 159 | "output_type": "stream", 160 | "name": "stdout", 161 | "text": [ 162 | "欢迎来到Onboard,真实的一线经验,走新的投资思考。 我是Monica,我是高宁,我们一起聊聊软件如何改变世界。 大家好,欢迎来到Onboard,我是Monica。 自从OpenAI发布的ChatGBT掀起了席卷世界的AI热潮, 不到三个月就积累了超过一亿的越活用户,超过1300万的日活用户。 真的是展现了AI让人惊叹的能力,也让很多人直呼这就是下一个互联网的未来。 有不少观众都说希望我们再做一期AI的讨论,于是这次硬核讨论就来了。 这次我们请来了Google Brain的研究员雪芝, 她是Google大语言模型POM,Pathway Language Model的作者之一。 要知道这个模型的参数量是GPT-3的三倍还多。 另外还有两位AI产品大牛,一位来自著名的Stable Diffusion背后的商业公司Stability AI, 另一位来自某硅谷科技大厂,也曾在吴恩达教授的Landing AI中担任产品负责人。 此外,Monica还邀请到一位一直关注AI的投资人朋友Bill,当做我的特邀共同主持嘉宾。 我们主要讨论几个话题,一方面从研究的视角,最前沿的研究者在关注什么? 现在的技术的天花板和未来大的变量可能会在哪里? 从产品和商业的角度,什么是一个好的AI产品? 整个生态可能随着技术有怎样的演变? 更重要的,我们又能从上一波AI的创业热潮中学到什么? 最后,Monica和Bill还会从投资人的视角做一个回顾、总结和畅想。 这里还有一个小的update,在本集发布的时候, Google也对爆发式增长的ChatGPT做出了回应, 正在测试一个基于Lambda模型的聊天机器人ApprenticeBot。 正式发布后会有怎样的惊喜?我们都拭目以待。 AI无疑是未来几年最令人兴奋的变量之一, Monica也希望未来能邀请到更多一线从业者从不同角度讨论这个话题。 不论是想要做创业、研究、产品还是投资的同学, 希望这些对话对于大家了解这些技术演进、商业的可能, 甚至未来对于我们每个人、每个社会意味着什么, 都能引发一些思考,提供一些启发。 这次的讨论有些技术硬核,需要各位对生成式AI、大模型都有一些基础了解。 讨论中涉及到的论文和重要概念,也会总结在本集的简介中,供大家复习参考。 几位嘉宾在北美工作生活多年,夹杂英文在所难免,也请大家体谅了。 欢迎来到未来,大家enjoy!\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 168 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file,\n", 169 | " prompt=\"这是一段Onboard播客的内容。\")\n", 170 | "print(transcript['text'])" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 7, 176 | "metadata": { 177 | "id": "8C5E1qIJ93cL", 178 | "outputId": "44c094af-f3d0-4e7b-8fa5-79ef825c2d30", 179 | "colab": { 180 | "base_uri": "https://localhost:8080/" 181 | } 182 | }, 183 | "outputs": [ 184 | { 185 | "output_type": "stream", 186 | "name": "stdout", 187 | "text": [ 188 | "欢迎来到Onboard,真实的一线经验,走新的投资思考。我是Monica。 我是高宁。我们一起聊聊软件如何改变世界。 大家好,欢迎来到Onboard,我是Monica。 自从OpenAI发布的ChatGPT掀起了席卷世界的AI热潮,不到三个月就积累了超过一亿的越活用户,超过1300万的日活用户。 真的是展现了AI让人惊叹的能力,也让很多人直呼这就是下一个互联网的未来。 有不少观众都说希望我们再做一期AI的讨论,于是这次硬核讨论就来了。 这次我们请来了Google Brain的研究员雪芝,她是Google大语言模型PALM Pathways Language Model的作者之一。 要知道,这个模型的参数量是GPT-3的三倍还多。 另外还有两位AI产品大牛,一位来自著名的Stable Diffusion背后的商业公司Stability AI, 另一位来自某硅谷科技大厂,也曾在吴恩达教授的Landing AI中担任产品负责人。 此外,Monica还邀请到一位一直关注AI的投资人朋友Bill当作我的特邀共同主持嘉宾。 我们主要讨论几个话题,一方面从研究的视角,最前沿的研究者在关注什么? 现在的技术的天花板和未来大的变量可能会在哪里? 从产品和商业的角度,什么是一个好的AI产品? 整个生态可能随着技术有怎样的演变? 更重要的,我们又能从上一波AI的创业热潮中学到什么? 最后,Monica和Bill还会从投资人的视角做一个回顾、总结和畅想。 这里还有一个小的update,在本集发布的时候,Google也对爆发式增长的Chat GPT做出了回应。 正在测试一个基于Lambda模型的聊天机器人ApprenticeBot。 证实发布后会有怎样的惊喜,我们都拭目以待。 AI无疑是未来几年最令人兴奋的变量之一。 Monica也希望未来能邀请到更多一线从业者从不同角度讨论这个话题。 不论是想要做创业、研究、产品还是投资的同学, 希望这些对话对于大家了解这些技术演进、商业的可能,甚至未来对于我们每个人、每个社会意味着什么都能引发一些思考,提供一些启发。 这次的讨论有些技术硬核,需要各位对生成式AI大模型都有一些基础了解。 讨论中涉及到的论文和重要概念也会总结在本集的简介中,供大家复习参考。 几位嘉宾在北美工作生活多年,夹杂英文在所难免,也请大家体谅了。 欢迎来到未来,大家enjoy!\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 194 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file,\n", 195 | " prompt=\"这是一段Onboard播客,里面会聊到ChatGPT以及PALM这个大语言模型。这个模型也叫做Pathways Language Model。\")\n", 196 | "print(transcript['text'])" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 8, 202 | "metadata": { 203 | "id": "_xCbVQJE93cM", 204 | "outputId": "75d30627-224f-47e4-823b-d31f67451a36", 205 | "colab": { 206 | "base_uri": "https://localhost:8080/" 207 | } 208 | }, 209 | "outputs": [ 210 | { 211 | "output_type": "stream", 212 | "name": "stdout", 213 | "text": [ 214 | "1\n", 215 | "00:00:01,000 --> 00:00:07,000\n", 216 | "欢迎来到Onboard,真实的一线经验,走新的投资思考。我是Monica。\n", 217 | "\n", 218 | "2\n", 219 | "00:00:07,000 --> 00:00:11,000\n", 220 | "我是高宁。我们一起聊聊软件如何改变世界。\n", 221 | "\n", 222 | "3\n", 223 | "00:00:15,000 --> 00:00:17,000\n", 224 | "大家好,欢迎来到Onboard,我是Monica。\n", 225 | "\n", 226 | "4\n", 227 | "00:00:17,000 --> 00:00:28,000\n", 228 | "自从OpenAI发布的ChatGBT掀起了席卷世界的AI热潮,不到三个月就积累了超过一亿的越活用户,超过1300万的日活用户。\n", 229 | "\n", 230 | "5\n", 231 | "00:00:28,000 --> 00:00:34,000\n", 232 | "真的是展现了AI让人惊叹的能力,也让很多人直呼这就是下一个互联网的未来。\n", 233 | "\n", 234 | "6\n", 235 | "00:00:34,000 --> 00:00:41,000\n", 236 | "有不少观众都说希望我们再做一期AI的讨论,于是这次硬核讨论就来了。\n", 237 | "\n", 238 | "7\n", 239 | "00:00:41,000 --> 00:00:49,000\n", 240 | "这次我们请来了Google Brain的研究员雪芝,她是Google大语言模型PALM Pathways Language Model的作者之一。\n", 241 | "\n", 242 | "8\n", 243 | "00:00:49,000 --> 00:00:53,000\n", 244 | "要知道,这个模型的参数量是GPT-3的三倍还多。\n", 245 | "\n", 246 | "9\n", 247 | "00:00:53,000 --> 00:01:01,000\n", 248 | "另外还有两位AI产品大牛,一位来自著名的Stable Diffusion背后的商业公司Surbility AI,\n", 249 | "\n", 250 | "10\n", 251 | "00:01:01,000 --> 00:01:08,000\n", 252 | "另一位来自某硅谷科技大厂,也曾在吴恩达教授的Landing AI中担任产品负责人。\n", 253 | "\n", 254 | "11\n", 255 | "00:01:08,000 --> 00:01:15,000\n", 256 | "此外,Monica还邀请到一位一直关注AI的投资人朋友Bill当作我的特邀共同主持嘉宾。\n", 257 | "\n", 258 | "12\n", 259 | "00:01:15,000 --> 00:01:22,000\n", 260 | "我们主要讨论几个话题,一方面从研究的视角,最前沿的研究者在关注什么?\n", 261 | "\n", 262 | "13\n", 263 | "00:01:22,000 --> 00:01:26,000\n", 264 | "现在的技术的天花板和未来大的变量可能会在哪里?\n", 265 | "\n", 266 | "14\n", 267 | "00:01:26,000 --> 00:01:30,000\n", 268 | "从产品和商业的角度,什么是一个好的AI产品?\n", 269 | "\n", 270 | "15\n", 271 | "00:01:30,000 --> 00:01:33,000\n", 272 | "整个生态可能随着技术有怎样的演变?\n", 273 | "\n", 274 | "16\n", 275 | "00:01:33,000 --> 00:01:38,000\n", 276 | "更重要的,我们又能从上一波AI的创业热潮中学到什么?\n", 277 | "\n", 278 | "17\n", 279 | "00:01:38,000 --> 00:01:44,000\n", 280 | "最后,Monica和Bill还会从投资人的视角做一个回顾、总结和畅想。\n", 281 | "\n", 282 | "18\n", 283 | "00:01:44,000 --> 00:01:51,000\n", 284 | "这里还有一个小的update,在本集发布的时候,Google也对爆发式增长的Chat GPT做出了回应。\n", 285 | "\n", 286 | "19\n", 287 | "00:01:51,000 --> 00:01:56,000\n", 288 | "正在测试一个基于Lambda模型的聊天机器人ApprenticeBot。\n", 289 | "\n", 290 | "20\n", 291 | "00:01:56,000 --> 00:02:00,000\n", 292 | "证实发布后会有怎样的惊喜,我们都拭目以待。\n", 293 | "\n", 294 | "21\n", 295 | "00:02:00,000 --> 00:02:04,000\n", 296 | "AI无疑是未来几年最令人兴奋的变量之一。\n", 297 | "\n", 298 | "22\n", 299 | "00:02:04,000 --> 00:02:10,000\n", 300 | "Monica也希望未来能邀请到更多一线从业者从不同角度讨论这个话题。\n", 301 | "\n", 302 | "23\n", 303 | "00:02:10,000 --> 00:02:15,000\n", 304 | "不论是想要做创业、研究、产品还是投资的同学,\n", 305 | "\n", 306 | "24\n", 307 | "00:02:15,000 --> 00:02:27,000\n", 308 | "希望这些对话对于大家了解这些技术演进、商业的可能,甚至未来对于我们每个人、每个社会意味着什么都能引发一些思考,提供一些启发。\n", 309 | "\n", 310 | "25\n", 311 | "00:02:27,000 --> 00:02:34,000\n", 312 | "这次的讨论有些技术硬核,需要各位对生成式AI大模型都有一些基础了解。\n", 313 | "\n", 314 | "26\n", 315 | "00:02:34,000 --> 00:02:41,000\n", 316 | "讨论中涉及到的论文和重要概念也会总结在本集的简介中,供大家复习参考。\n", 317 | "\n", 318 | "27\n", 319 | "00:02:41,000 --> 00:02:47,000\n", 320 | "几位嘉宾在北美工作生活多年,夹杂英文在所难免,也请大家体谅了。\n", 321 | "\n", 322 | "28\n", 323 | "00:02:47,000 --> 00:03:12,000\n", 324 | "欢迎来到未来,大家enjoy!\n", 325 | "\n", 326 | "\n", 327 | "\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 333 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file, response_format=\"srt\",\n", 334 | " prompt=\"这是一段Onboard播客,里面会聊到PALM这个大语言模型。这个模型也叫做Pathways Language Model。\")\n", 335 | "print(transcript)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": null, 341 | "metadata": { 342 | "id": "3sAmJkVE93cM", 343 | "outputId": "a7d04b61-c097-4278-9c93-ba679fac00bc" 344 | }, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "WEBVTT\n", 351 | "\n", 352 | "00:00:01.000 --> 00:00:07.000\n", 353 | "欢迎来到Onboard,真实的一线经验,走新的投资思考。我是Monica。\n", 354 | "\n", 355 | "00:00:07.000 --> 00:00:11.000\n", 356 | "我是高宁。我们一起聊聊软件如何改变世界。\n", 357 | "\n", 358 | "00:00:15.000 --> 00:00:17.000\n", 359 | "大家好,欢迎来到Onboard,我是Monica。\n", 360 | "\n", 361 | "00:00:17.000 --> 00:00:28.000\n", 362 | "自从OpenAI发布的ChatGBT掀起了席卷世界的AI热潮,不到三个月就积累了超过一亿的越活用户,超过1300万的日活用户。\n", 363 | "\n", 364 | "00:00:28.000 --> 00:00:34.000\n", 365 | "真的是展现了AI让人惊叹的能力,也让很多人直呼这就是下一个互联网的未来。\n", 366 | "\n", 367 | "00:00:34.000 --> 00:00:41.000\n", 368 | "有不少观众都说希望我们再做一期AI的讨论,于是这次硬核讨论就来了。\n", 369 | "\n", 370 | "00:00:41.000 --> 00:00:49.000\n", 371 | "这次我们请来了Google Brain的研究员雪芝,她是Google大语言模型PALM Pathways Language Model的作者之一。\n", 372 | "\n", 373 | "00:00:49.000 --> 00:00:53.000\n", 374 | "要知道,这个模型的参数量是GPT-3的三倍还多。\n", 375 | "\n", 376 | "00:00:53.000 --> 00:01:01.000\n", 377 | "另外还有两位AI产品大牛,一位来自著名的Stable Diffusion背后的商业公司Surbility AI,\n", 378 | "\n", 379 | "00:01:01.000 --> 00:01:08.000\n", 380 | "另一位来自某硅谷科技大厂,也曾在吴恩达教授的Landing AI中担任产品负责人。\n", 381 | "\n", 382 | "00:01:08.000 --> 00:01:15.000\n", 383 | "此外,Monica还邀请到一位一直关注AI的投资人朋友Bill当作我的特邀共同主持嘉宾。\n", 384 | "\n", 385 | "00:01:15.000 --> 00:01:22.000\n", 386 | "我们主要讨论几个话题,一方面从研究的视角,最前沿的研究者在关注什么?\n", 387 | "\n", 388 | "00:01:22.000 --> 00:01:26.000\n", 389 | "现在的技术的天花板和未来大的变量可能会在哪里?\n", 390 | "\n", 391 | "00:01:26.000 --> 00:01:30.000\n", 392 | "从产品和商业的角度,什么是一个好的AI产品?\n", 393 | "\n", 394 | "00:01:30.000 --> 00:01:33.000\n", 395 | "整个生态可能随着技术有怎样的演变?\n", 396 | "\n", 397 | "00:01:33.000 --> 00:01:38.000\n", 398 | "更重要的,我们又能从上一波AI的创业热潮中学到什么?\n", 399 | "\n", 400 | "00:01:38.000 --> 00:01:44.000\n", 401 | "最后,Monica和Bill还会从投资人的视角做一个回顾、总结和畅想。\n", 402 | "\n", 403 | "00:01:44.000 --> 00:01:51.000\n", 404 | "这里还有一个小的update,在本集发布的时候,Google也对爆发式增长的Chat GPT做出了回应。\n", 405 | "\n", 406 | "00:01:51.000 --> 00:01:56.000\n", 407 | "正在测试一个基于Lambda模型的聊天机器人ApprenticeBot。\n", 408 | "\n", 409 | "00:01:56.000 --> 00:02:00.000\n", 410 | "证实发布后会有怎样的惊喜,我们都拭目以待。\n", 411 | "\n", 412 | "00:02:00.000 --> 00:02:04.000\n", 413 | "AI无疑是未来几年最令人兴奋的变量之一。\n", 414 | "\n", 415 | "00:02:04.000 --> 00:02:10.000\n", 416 | "Monica也希望未来能邀请到更多一线从业者从不同角度讨论这个话题。\n", 417 | "\n", 418 | "00:02:10.000 --> 00:02:15.000\n", 419 | "不论是想要做创业、研究、产品还是投资的同学,\n", 420 | "\n", 421 | "00:02:15.000 --> 00:02:27.000\n", 422 | "希望这些对话对于大家了解这些技术演进、商业的可能,甚至未来对于我们每个人、每个社会意味着什么都能引发一些思考,提供一些启发。\n", 423 | "\n", 424 | "00:02:27.000 --> 00:02:34.000\n", 425 | "这次的讨论有些技术硬核,需要各位对生成式AI大模型都有一些基础了解。\n", 426 | "\n", 427 | "00:02:34.000 --> 00:02:41.000\n", 428 | "讨论中涉及到的论文和重要概念也会总结在本集的简介中,供大家复习参考。\n", 429 | "\n", 430 | "00:02:41.000 --> 00:02:47.000\n", 431 | "几位嘉宾在北美工作生活多年,夹杂英文在所难免,也请大家体谅了。\n", 432 | "\n", 433 | "00:02:47.000 --> 00:03:12.000\n", 434 | "欢迎来到未来,大家enjoy!\n", 435 | "\n", 436 | "\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 442 | "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file, response_format=\"vtt\",\n", 443 | " prompt=\"这是一段Onboard播客,里面会聊到PALM这个大语言模型。这个模型也叫做Pathways Language Model。\")\n", 444 | "print(transcript)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": { 450 | "id": "wV0eHUoG93cN" 451 | }, 452 | "source": [ 453 | "## 转录中顺便翻译" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 9, 459 | "metadata": { 460 | "id": "yB_MSR3c93cN", 461 | "outputId": "cf341ccb-6d99-474b-a903-1d148709fec1", 462 | "colab": { 463 | "base_uri": "https://localhost:8080/" 464 | } 465 | }, 466 | "outputs": [ 467 | { 468 | "output_type": "stream", 469 | "name": "stdout", 470 | "text": [ 471 | "Welcome to Onboard, a new way to think about investing. I am Monica. I am Gao Ning. Let's talk about how software can change the world. Hello everyone, welcome to Onboard. I am Monica. Since the release of ChatGPT, which is an open AI, it has aroused the hype of AI in the world. In less than three months, it has accumulated more than 100 million active users and more than 13 million active users. It really shows the ability of AI to amaze people. It also makes many people say that this is the future of the next Internet. Many viewers said that they wanted us to do another AI discussion. So this discussion came. This time we invited a researcher from Google Brain, Xue Zhi. He is one of the authors of Google's large-scale language model PaLM, Pathways Language Model. You should know that the number of parameters of this model is three times more than GPT-3. There are also two AI product bigwigs, a commercial company behind the famous Stable Diffusion, Stability AI. Another one is from a Silicon Valley technology factory and was also the product manager in Professor Wu Wenda's Landing AI. In addition, Monica also invited a friend of AI who has been paying attention to AI, Bill, as my special guest host. We mainly discuss several topics. On the one hand, from the perspective of research, what are the most cutting-edge researchers paying attention to? Where are the cutting-edge technologies and the large variables of the future? From the perspective of products and business, what is a good AI product? What kind of evolution may the whole state follow? More importantly, what can we learn from the last wave of AI entrepreneurship? Finally, Monica and Bill will also make a review, summary and reflection from the perspective of investors. Here is a small update. When this issue was released, Google also responded to the explosive growth of ChatGPT. We are testing an Apprentice Bot based on the Lambda model. What kind of surprises will be released? We are looking forward to it. AI is undoubtedly one of the most exciting variables in the coming years. Monica also hopes to invite more frontline entrepreneurs to discuss this topic from different angles. Whether you want to start a business, research, product or investment, I hope these conversations will help you understand the possibilities of these technical horizons and business. Even in the future, it can cause some thoughts and inspire us to think about what it means to each person and each society. This discussion is a bit technical, and requires you to have some basic understanding of the biometric AI model. The discussion will also be summarized in this episode's summary, which is for your reference. You have been working in North America for many years, and your English is not good. Please understand. Welcome to the future. Enjoy. Let me give you a brief introduction. Some of your past experiences. A fun fact, your favorite TV show.\n" 472 | ] 473 | } 474 | ], 475 | "source": [ 476 | "audio_file= open(\"./data/generative_ai_topics_clip.mp3\", \"rb\")\n", 477 | "translated_prompt=\"\"\"This is a podcast discussing ChatGPT and PaLM model.\n", 478 | "The full name of PaLM is Pathways Language Model.\"\"\"\n", 479 | "transcript = openai.Audio.translate(\"whisper-1\", audio_file,\n", 480 | " prompt=translated_prompt)\n", 481 | "print(transcript['text'])" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": null, 487 | "metadata": { 488 | "id": "Z-F3X6cV93cN", 489 | "outputId": "8dee9103-94d0-45b0-e03f-43b0b6ada709" 490 | }, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | "Welcome to Onboard. Real first-line experience. New investment thinking. I am Monica. I am Gao Ning. Let's talk about how software can change the world. Hello everyone, welcome to Onboard. I am Monica. Since the release of ChatGPT by OpenAI, the world's AI has been in a frenzy. In less than three months, it has accumulated more than 100 million active users, and more than 13 million active users. It really shows the amazing ability of AI. It also makes many people say that this is the future of the next Internet. Many viewers said that they wanted us to do another AI discussion. So this discussion came. This time we invited a researcher from Google Brain, Xue Zhi. He is one of the authors of Google's large-scale model PaLM, Pathways Language Model. You should know that the number of parameters of this model is three times more than ChatGPT-3. In addition, there are two AI product big cows. One is from the famous company behind Stable Diffusion, Stability AI. The other is from a Silicon Valley technology factory. He was also the product manager in Professor Wu Wenda's Landing AI. In addition, Monica also invited a friend of AI who has been paying attention to AI, Bill, as my special guest host. We mainly discuss several topics. On the one hand, from the perspective of research, what are the most cutting-edge researchers paying attention to? Where are the cutting-edge technologies and the large variables of the future? From the perspective of products and business, what is a good AI product? What kind of evolution may the whole state follow? More importantly, what can we learn from the previous wave of AI entrepreneurship? Finally, Monica and Bill will also make a review, summary and reflection from the perspective of investors. Here is a small update. When this issue was released, Google also responded to the explosive growth of ChatGPT. We are testing an Apprentice Bot based on Lambda model. What kind of surprises will be released? We are looking forward to it. AI is undoubtedly one of the most exciting variables in the coming years. Monica also hopes to invite more first-line entrepreneurs to discuss this topic from different angles. Whether you want to do entrepreneurship, research, product or investment, I hope these conversations will help you understand the possibilities of these technical horizons and business. Even in the future, it can cause some thoughts and inspire us to think about what it means to each person and each society. This discussion is a bit technical, and requires you to have some basic understanding of the biometric AI model. The papers and important concepts involved in the discussion will also be summarized in this episode's summary, which is for your reference. You have worked in North America for many years, and you may have some English mistakes. Please understand. Welcome to the future. Enjoy. Let me give you a brief introduction. Some of your past experiences. A fun fact. Using an AI to represent the world is now palped.\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "print(transcript['text'])" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": { 507 | "id": "kZ2dOJxl93cO" 508 | }, 509 | "source": [ 510 | "## 分割音频转录" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 13, 516 | "metadata": { 517 | "id": "n3xZu3Wi93cO", 518 | "colab": { 519 | "base_uri": "https://localhost:8080/" 520 | }, 521 | "outputId": "e52795e3-43d5-4536-fc5f-d034654d3b50" 522 | }, 523 | "outputs": [ 524 | { 525 | "output_type": "stream", 526 | "name": "stdout", 527 | "text": [ 528 | "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", 529 | "Collecting pydub\n", 530 | " Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", 531 | "Installing collected packages: pydub\n", 532 | "Successfully installed pydub-0.25.1\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "%pip install pydub" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 14, 543 | "metadata": { 544 | "id": "f9MJblPT93cO" 545 | }, 546 | "outputs": [], 547 | "source": [ 548 | "from pydub import AudioSegment\n", 549 | "\n", 550 | "podcast = AudioSegment.from_mp3(\"./data/generative_ai_topics_long.mp3\")\n", 551 | "\n", 552 | "# PyDub handles time in milliseconds\n", 553 | "ten_minutes = 15 * 60 * 1000\n", 554 | "\n", 555 | "total_length = len(podcast)\n", 556 | "\n", 557 | "start = 0\n", 558 | "index = 0\n", 559 | "while start < total_length:\n", 560 | " end = start + ten_minutes\n", 561 | " if end < total_length:\n", 562 | " chunk = podcast[start:end]\n", 563 | " else:\n", 564 | " chunk = podcast[start:]\n", 565 | " with open(f\"./data/generative_ai_topics_long_{index}.mp3\", \"wb\") as f:\n", 566 | " chunk.export(f, format=\"mp3\")\n", 567 | " start = end\n", 568 | " index += 1" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 15, 574 | "metadata": { 575 | "id": "x-Ky7FC-93cO" 576 | }, 577 | "outputs": [], 578 | "source": [ 579 | "original_prompt = \"这是一段Onboard播客,里面会聊到ChatGPT以及PALM这个大语言模型。这个模型也叫做Pathways Language Model。\\n\\n\"\n", 580 | "prompt = original_prompt\n", 581 | "for i in range(index):\n", 582 | " clip = f\"./data/generative_ai_topics_long_{i}.mp3\"\n", 583 | " audio_file= open(clip, \"rb\")\n", 584 | " transcript = openai.Audio.transcribe(\"whisper-1\", audio_file,\n", 585 | " prompt=prompt)\n", 586 | " # mkdir ./data/transcripts if not exists\n", 587 | " if not os.path.exists(\"./data/transcripts\"):\n", 588 | " os.makedirs(\"./data/transcripts\")\n", 589 | " # write to file\n", 590 | " with open(f\"./data/transcripts/generative_ai_topics_long_{i}.txt\", \"w\") as f:\n", 591 | " f.write(transcript['text'])\n", 592 | " # get last sentence of the transcript\n", 593 | " sentences = transcript['text'].split(\"。\")\n", 594 | " prompt = original_prompt + sentences[-1]\n" 595 | ] 596 | }, 597 | { 598 | "cell_type": "markdown", 599 | "metadata": { 600 | "id": "soibBzqb93cP" 601 | }, 602 | "source": [ 603 | "## 用ChatGPT总结" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "source": [ 609 | "%pip install llama-index==0.5.26\n", 610 | "%pip install langchain" 611 | ], 612 | "metadata": { 613 | "id": "QzzPX95zSMpi" 614 | }, 615 | "execution_count": null, 616 | "outputs": [] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "source": [ 621 | "%pip install spacy\n", 622 | "%run -m spacy download zh_core_web_sm" 623 | ], 624 | "metadata": { 625 | "colab": { 626 | "base_uri": "https://localhost:8080/" 627 | }, 628 | "id": "M9MhM4RNRa1J", 629 | "outputId": "0bc5e761-d169-4c5a-91d3-43f224c75e58" 630 | }, 631 | "execution_count": 2, 632 | "outputs": [ 633 | { 634 | "output_type": "stream", 635 | "name": "stdout", 636 | "text": [ 637 | "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", 638 | "Requirement already satisfied: spacy in /usr/local/lib/python3.10/dist-packages (3.5.2)\n", 639 | "Requirement already satisfied: spacy-legacy<3.1.0,>=3.0.11 in /usr/local/lib/python3.10/dist-packages (from spacy) (3.0.12)\n", 640 | "Requirement already satisfied: spacy-loggers<2.0.0,>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (1.0.4)\n", 641 | "Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (1.0.9)\n", 642 | "Requirement already satisfied: cymem<2.1.0,>=2.0.2 in /usr/local/lib/python3.10/dist-packages (from spacy) (2.0.7)\n", 643 | "Requirement already satisfied: preshed<3.1.0,>=3.0.2 in /usr/local/lib/python3.10/dist-packages (from spacy) (3.0.8)\n", 644 | "Requirement already satisfied: thinc<8.2.0,>=8.1.8 in /usr/local/lib/python3.10/dist-packages (from spacy) (8.1.9)\n", 645 | "Requirement already satisfied: wasabi<1.2.0,>=0.9.1 in /usr/local/lib/python3.10/dist-packages (from spacy) (1.1.1)\n", 646 | "Requirement already satisfied: srsly<3.0.0,>=2.4.3 in /usr/local/lib/python3.10/dist-packages (from spacy) (2.4.6)\n", 647 | "Requirement already satisfied: catalogue<2.1.0,>=2.0.6 in /usr/local/lib/python3.10/dist-packages (from spacy) (2.0.8)\n", 648 | "Requirement already satisfied: typer<0.8.0,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (0.7.0)\n", 649 | "Requirement already satisfied: pathy>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (0.10.1)\n", 650 | "Requirement already satisfied: smart-open<7.0.0,>=5.2.1 in /usr/local/lib/python3.10/dist-packages (from spacy) (6.3.0)\n", 651 | "Requirement already satisfied: tqdm<5.0.0,>=4.38.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (4.65.0)\n", 652 | "Requirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (1.22.4)\n", 653 | "Requirement already satisfied: requests<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (2.27.1)\n", 654 | "Requirement already satisfied: pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4 in /usr/local/lib/python3.10/dist-packages (from spacy) (1.10.7)\n", 655 | "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from spacy) (3.1.2)\n", 656 | "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from spacy) (67.7.2)\n", 657 | "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (23.1)\n", 658 | "Requirement already satisfied: langcodes<4.0.0,>=3.2.0 in /usr/local/lib/python3.10/dist-packages (from spacy) (3.3.0)\n", 659 | "Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4->spacy) (4.5.0)\n", 660 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.13.0->spacy) (1.26.15)\n", 661 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.13.0->spacy) (2022.12.7)\n", 662 | "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.13.0->spacy) (2.0.12)\n", 663 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.13.0->spacy) (3.4)\n", 664 | "Requirement already satisfied: blis<0.8.0,>=0.7.8 in /usr/local/lib/python3.10/dist-packages (from thinc<8.2.0,>=8.1.8->spacy) (0.7.9)\n", 665 | "Requirement already satisfied: confection<1.0.0,>=0.0.1 in /usr/local/lib/python3.10/dist-packages (from thinc<8.2.0,>=8.1.8->spacy) (0.0.4)\n", 666 | "Requirement already satisfied: click<9.0.0,>=7.1.1 in /usr/local/lib/python3.10/dist-packages (from typer<0.8.0,>=0.3.0->spacy) (8.1.3)\n", 667 | "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->spacy) (2.1.2)\n", 668 | "\u001b[38;5;2m✔ Download and installation successful\u001b[0m\n", 669 | "You can now load the package via spacy.load('zh_core_web_sm')\n" 670 | ] 671 | } 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": 5, 677 | "metadata": { 678 | "id": "1kxH0Jyb93cP", 679 | "outputId": "cd796a91-752e-4945-bad3-5af8c77da450", 680 | "colab": { 681 | "base_uri": "https://localhost:8080/" 682 | } 683 | }, 684 | "outputs": [ 685 | { 686 | "output_type": "stream", 687 | "name": "stderr", 688 | "text": [ 689 | "WARNING:llama_index.llm_predictor.base:Unknown max input size for gpt-3.5-turbo, using defaults.\n" 690 | ] 691 | }, 692 | { 693 | "output_type": "stream", 694 | "name": "stdout", 695 | "text": [ 696 | "播客讨论了AI的未来发展趋势、技术的天花板、好的AI产品应该具备的特点以及整个生态可能随着技术演变的变化。同时,回顾了上一波AI的创业热潮中的经验教训,并从投资人的视角做了一个回顾、总结和畅想。另外,还讨论了生成式AI大模型、Diffusion app在手机上的成功编译、AI和开源模型的应用、Google POM语言模型的介绍以及其与GPT-3的区别。最后,讨论了数据在微调任务中的重要性以及零样本、少样本和微调方法之间的差异。\n" 697 | ] 698 | } 699 | ], 700 | "source": [ 701 | "from langchain.chat_models import ChatOpenAI\n", 702 | "from langchain.text_splitter import SpacyTextSplitter\n", 703 | "from llama_index import GPTListIndex, LLMPredictor, ServiceContext, SimpleDirectoryReader\n", 704 | "from llama_index.node_parser import SimpleNodeParser\n", 705 | "\n", 706 | "# define LLM\n", 707 | "llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\", max_tokens=1024))\n", 708 | "\n", 709 | "text_splitter = SpacyTextSplitter(pipeline=\"zh_core_web_sm\", chunk_size = 2048)\n", 710 | "parser = SimpleNodeParser(text_splitter=text_splitter)\n", 711 | "documents = SimpleDirectoryReader('./data/transcripts').load_data()\n", 712 | "nodes = parser.get_nodes_from_documents(documents)\n", 713 | "\n", 714 | "service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)\n", 715 | "\n", 716 | "list_index = GPTListIndex(nodes=nodes, service_context=service_context)\n", 717 | "\n", 718 | "response = list_index.query(\"请你总结一下我们的播客内容:\", response_mode=\"tree_summarize\")\n", 719 | "print(response)" 720 | ] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "execution_count": 6, 725 | "metadata": { 726 | "id": "6pfQVzJg93cP", 727 | "outputId": "1b68944e-e40e-4ae0-b7ed-c60248bb950a", 728 | "colab": { 729 | "base_uri": "https://localhost:8080/" 730 | } 731 | }, 732 | "outputs": [ 733 | { 734 | "output_type": "stream", 735 | "name": "stdout", 736 | "text": [ 737 | "播客讨论了AI的未来发展趋势、技术的天花板、好的AI产品应该具备的特点以及整个生态可能随着技术演变的变化。同时,回顾了上一波AI的创业热潮中的经验教训,并从投资人的视角做了一个回顾、总结和畅想。另外,还讨论了生成式AI大模型、Diffusion app在手机上的成功编译、AI和开源模型的应用、Google POM语言模型的介绍以及其与GPT-3的区别。最后,讨论了数据在微调任务中的重要性以及零样本、少样本和微调方法之间的差异。\n" 738 | ] 739 | } 740 | ], 741 | "source": [ 742 | "print(response)" 743 | ] 744 | } 745 | ], 746 | "metadata": { 747 | "kernelspec": { 748 | "display_name": "geektime", 749 | "language": "python", 750 | "name": "python3" 751 | }, 752 | "language_info": { 753 | "name": "python", 754 | "version": "3.10.9" 755 | }, 756 | "orig_nbformat": 4, 757 | "colab": { 758 | "provenance": [], 759 | "include_colab_link": true 760 | } 761 | }, 762 | "nbformat": 4, 763 | "nbformat_minor": 0 764 | } --------------------------------------------------------------------------------