File size: 6,397 Bytes
0b75c79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
{
"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
}
|