{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1.1\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "! pip3 install -qU markdownify langchain-upstage rank_bm25 python-dotenv langchain_chroma langchain" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%load_ext dotenv\n", "%dotenv" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from langchain_chroma import Chroma\n", "from langchain_upstage import UpstageEmbeddings\n", "from langchain.docstore.document import Document\n", "\n", "sample_text_list = [\n", " \"마법사의 나무는 마법 지팡이의 재료 중 최상급의 재료로 알려져 있다\",\n", " \"There are Weasleys' Wizard Wheezes nearby the Diagon Alley\",\n", " \"Diangon Alley is a place where you can buy magic wands\",\n", " \"Hogwart is a school for witches and wizards\",\n", "]\n", "sample_docs = [Document(page_content=text) for text in sample_text_list]\n", "vectorstore = Chroma.from_documents(\n", " documents=sample_docs,\n", " embedding=UpstageEmbeddings(model=\"solar-embedding-1-large\"),\n", ")\n", "retriever = vectorstore.as_retriever()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "from langchain_core.prompts import PromptTemplate\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_upstage import ChatUpstage\n", "\n", "\n", "llm = ChatUpstage()\n", "\n", "prompt_template = PromptTemplate.from_template(\n", " \"\"\"\n", " You are best D&D host. And user is playing D&D game with you.\n", " Please answer in a exciting tone as a host.\n", " User has to make choices in the game.\n", " User has 10 life points and 10 gold coins.\n", " Please make scenario of most interesting event from the following context, and provide diverse choices which user can select.\n", " For each round, you should provide at least 3 choices and total round should be less than 50.\n", " You should explain situation and choices in detail.\n", " If someone's conversation is included in the scenario, please express it in colloquial terms that fit the person's character as much as possible.\n", " Choices should be diverse and interesting, and each choice will reduce or increase user's life points or gold coins.\n", " If user make appropriate decision, user's life points or gold coins will increase.\n", " If user make inappropriate decision, user's life points or gold coins will decrease and decreasing amount should not be larger than amount of user owned.\n", " When user select a choice, you should provide the result of the choice, but you should not show result before user select the choice.\n", " You should consider previous conversation and context to make the scenario.\n", " If user's life points or gold coins are less than 0, user will lose the game and get bad ending.\n", " Also when total round is more than 50, user will lose the game and get bad ending.\n", " If user's life points or gold coins are more than 20, user will win the game and get good ending.\n", " ---\n", " User Choice: {user_choice}\n", " ---\n", " Previous Conversation: {previous_conversation}\n", " ---\n", " Context: {context}\n", " \"\"\"\n", ")\n", "chain = prompt_template | llm | StrOutputParser()\n", "\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'마법사의 나무와 용의 심장으로 만든 지팡이를 사고 싶다는군요! 그것은 강력한 마법 아이템입니다. 이제 선택의 시간이 왔습니다. 이 지팡이를 어떻게 얻을 것인가요?\\n\\n1. **마법 상점을 방문하세요:** 마을의 마법 상점을 찾아가서 지팡이를 구매해보세요. 가격은 8골드 코인입니다.\\n2. **마법사로부터 구매하세요:** 마을에 있는 마법사에게 지팡이를 구매할 수 있는지 물어보세요. 하지만 가격은 10골드 코인으로 비싸집니다.\\n3. **지팡이를 제작하세요:** 재료와 마법 지식을 가지고 지팡이를 직접 제작해보세요. 하지만 이 선택은 5골드 코인을 소비하고, 제작에 성공할 확률은 50%입니다.\\n\\n어떤 선택을 하시겠습니까?'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user_choice = \"저는 마법사의 나무와 용의 심장으로 만든 지팡이를 사고 싶어요.\"\n", "sample_context = \"What is related information about {user_choice}?\"\n", "result_docs = retriever.invoke(sample_context)\n", "chain.invoke({ \"user_choice\": user_choice, \"previous_conversation\": \"\", \"context\": sample_context })" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }