{ "cells": [ { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate\n", "from langchain.llms import OpenAI\n", "from langchain.chat_models import ChatOpenAI\n", "\n", "from langchain.output_parsers import PydanticOutputParser\n", "from pydantic import BaseModel, Field, validator\n", "from typing import List" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "model_name = 'text-davinci-003'\n", "temperature = 0.7\n", "model = OpenAI(model_name=model_name, temperature=temperature)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# Define your desired data structure.\n", "class Joke(BaseModel):\n", " setup: str = Field(description=\"question to set up a joke\")\n", " punchline: str = Field(description=\"answer to resolve the joke\")\n", " \n", " # You can add custom validation logic easily with Pydantic.\n", " @validator('setup')\n", " def question_ends_with_question_mark(cls, field):\n", " if field[-1] != '?' and field[-1] != '?':\n", " raise ValueError(\"Badly formed question!\")\n", " return field" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "# Set up a parser + inject instructions into the prompt template.\n", "parser = PydanticOutputParser(pydantic_object=Joke)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "prompt = PromptTemplate(\n", " template=\"Answer the user query.\\n{format_instructions}\\n{query}\\n\",\n", " input_variables=[\"query\"],\n", " partial_variables={\"format_instructions\": parser.get_format_instructions()}\n", ")" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "prompt: text='Answer the user query.\\nThe output should be formatted as a JSON instance that conforms to the JSON schema below.\\n\\nAs an example, for the schema {\"properties\": {\"foo\": {\"title\": \"Foo\", \"description\": \"a list of strings\", \"type\": \"array\", \"items\": {\"type\": \"string\"}}}, \"required\": [\"foo\"]}}\\nthe object {\"foo\": [\"bar\", \"baz\"]} is a well-formatted instance of the schema. The object {\"properties\": {\"foo\": [\"bar\", \"baz\"]}} is not well-formatted.\\n\\nHere is the output schema:\\n```\\n{\"properties\": {\"setup\": {\"title\": \"Setup\", \"description\": \"question to set up a joke\", \"type\": \"string\"}, \"punchline\": {\"title\": \"Punchline\", \"description\": \"answer to resolve the joke\", \"type\": \"string\"}}, \"required\": [\"setup\", \"punchline\"]}\\n```\\n用中文回答,讲个笑话。\\n'\n" ] } ], "source": [ "# And a query intented to prompt a language model to populate the data structure.\n", "joke_query = \"用中文回答,讲个笑话。\"\n", "_input = prompt.format_prompt(query=joke_query)\n", "print(\"prompt:\", _input)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "output = model(_input.to_string())" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 setup='为什么大象不喜欢穿衣服?' punchline='因为衣服太小!'\n", "1 setup='为什么乌龟不能唱歌?' punchline='因为它没有嗓子!'\n", "2 setup='什么东西可以排列成一半?' punchline='一个鸡蛋!'\n", "3 setup='为什么老虎不吃素?' punchline='因为他不能把萝卜拔得出土。'\n", "4 setup='什么植物可以在水里生长?' punchline='水芹菜!'\n", "5 setup='什么可以把一只老虎变成猫?' punchline='把它放进冰箱!'\n", "6 setup='为什么恐龙不能坐飞机?' punchline='因为它没有票!'\n", "7 setup='为什么熊总喜欢去森林游玩?' punchline='因为他想要一个熊朋友!'\n", "8 setup='什么东西可以把一座山搬走?' punchline='风'\n", "9 setup='为什么有些人把面包放进微波炉里?' punchline='因为他们想把它变成“烤面包”。'\n" ] } ], "source": [ "for i in range(10):\n", " output = model(_input.to_string())\n", " print(i, parser.parse(output))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }