Spaces:
Sleeping
Sleeping
Commit
·
0fc716a
1
Parent(s):
32f787c
feat: initial commit
Browse files- .gitattributes +1 -0
- .gitignore +5 -0
- app.py +48 -54
- index/default__vector_store.json +3 -0
- index/docstore.json +3 -0
- index/graph_store.json +3 -0
- index/image__vector_store.json +3 -0
- index/index_store.json +3 -0
- requirements.txt +7 -1
- src/01_dwn.ipynb +107 -0
- src/02_llm.ipynb +119 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.venv
|
2 |
+
data
|
3 |
+
.DS_Store
|
4 |
+
__pycache__
|
5 |
+
.env
|
app.py
CHANGED
@@ -1,64 +1,58 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from llama_index.core import StorageContext, load_index_from_storage, Settings
|
4 |
+
from llama_index.llms.azure_openai import AzureOpenAI
|
5 |
+
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv(override=True)
|
9 |
+
|
10 |
+
api_key = os.getenv("AZURE_OPENAI_API_KEY")
|
11 |
+
api_version = "2024-05-01-preview"
|
12 |
+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
|
13 |
+
|
14 |
+
llm = AzureOpenAI(
|
15 |
+
model="gpt-4o",
|
16 |
+
deployment_name="gpt-4o",
|
17 |
+
api_key=api_key,
|
18 |
+
azure_endpoint=azure_endpoint,
|
19 |
+
api_version=api_version,
|
20 |
+
)
|
21 |
|
22 |
+
# You need to deploy your own embedding model as well as your own chat completion model
|
23 |
+
embed_model = AzureOpenAIEmbedding(
|
24 |
+
model="text-embedding-3-small",
|
25 |
+
deployment_name="text-embedding-3-small",
|
26 |
+
api_key=api_key,
|
27 |
+
azure_endpoint=azure_endpoint,
|
28 |
+
api_version=api_version,
|
29 |
+
)
|
30 |
|
31 |
+
Settings.llm = llm
|
32 |
+
Settings.embed_model = embed_model
|
33 |
|
34 |
+
# rebuild storage context
|
35 |
+
storage_context = StorageContext.from_defaults(persist_dir="./index")
|
36 |
|
37 |
+
# load index
|
38 |
+
index = load_index_from_storage(storage_context)
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
query_engine = index.as_query_engine(similarity_top_k=10)
|
|
|
41 |
|
42 |
+
# Function to handle chat messages with history
|
43 |
+
def echo(message, history):
|
44 |
+
context = "\n".join([f"User: {user_msg}\nBot: {bot_msg}" for user_msg, bot_msg in history])
|
45 |
+
full_context = f"{context}\nUser: {message}"
|
46 |
+
response = query_engine.query(full_context).response
|
47 |
+
history.append((message, response))
|
48 |
+
return response # history
|
49 |
|
|
|
|
|
|
|
50 |
demo = gr.ChatInterface(
|
51 |
+
fn=echo,
|
52 |
+
examples=[
|
53 |
+
"光源氏はどのような人物ですか?",
|
54 |
+
"夕顔はどのような人物ですか?"
|
55 |
+
],
|
56 |
+
title="Llama Index Chatbot",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
)
|
58 |
+
demo.launch()
|
|
|
|
|
|
index/default__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f7fe224b5643b9f72240d2512a9b9d8a2f667c604a76a515e43990bd6ac89881
|
3 |
+
size 56982497
|
index/docstore.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff15643f2195ab3def2669052b4e7b2a1f94907658cab0dc0a6d27860439da0d
|
3 |
+
size 11660400
|
index/graph_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e0a77744010862225c69da83c585f4f8a42fd551b044ce530dbb1eb6e16742c
|
3 |
+
size 18
|
index/image__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d17ed74c1649a438e518a8dc56a7772913dfe1ea7a7605bce069c63872431455
|
3 |
+
size 72
|
index/index_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6f99ab7691679df00c79231c7620b7b41e2b0eb374425547075a236beb35cd9d
|
3 |
+
size 137839
|
requirements.txt
CHANGED
@@ -1 +1,7 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|
3 |
+
bs4
|
4 |
+
llama_index
|
5 |
+
llama-index-embeddings-azure-openai
|
6 |
+
llama-index-llms-azure-openai
|
7 |
+
python-dotenv
|
src/01_dwn.ipynb
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"ename": "ModuleNotFoundError",
|
10 |
+
"evalue": "No module named 'bs4'",
|
11 |
+
"output_type": "error",
|
12 |
+
"traceback": [
|
13 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
14 |
+
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
15 |
+
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrequests\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbs4\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BeautifulSoup\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n",
|
16 |
+
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'bs4'"
|
17 |
+
]
|
18 |
+
}
|
19 |
+
],
|
20 |
+
"source": [
|
21 |
+
"import requests\n",
|
22 |
+
"from bs4 import BeautifulSoup\n",
|
23 |
+
"import os"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": null,
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"url = \"https://genji.dl.itc.u-tokyo.ac.jp/data/info.json\"\n",
|
33 |
+
"\n",
|
34 |
+
"response = requests.get(url).json()\n",
|
35 |
+
"\n",
|
36 |
+
"selections = response[\"selections\"]\n",
|
37 |
+
"\n",
|
38 |
+
"for selection in selections:\n",
|
39 |
+
"\n",
|
40 |
+
" members = selection[\"members\"]\n",
|
41 |
+
"\n",
|
42 |
+
" for member in members:\n",
|
43 |
+
"\n",
|
44 |
+
" aozora_urls = []\n",
|
45 |
+
"\n",
|
46 |
+
" for metadata in member[\"metadata\"]:\n",
|
47 |
+
"\n",
|
48 |
+
" if metadata[\"label\"] == \"aozora\":\n",
|
49 |
+
"\n",
|
50 |
+
" aozora_urls = metadata[\"value\"].split(\", \")\n",
|
51 |
+
"\n",
|
52 |
+
" for aozora_url in aozora_urls:\n",
|
53 |
+
"\n",
|
54 |
+
" filename = aozora_url.split(\"/\")[-1].split(\".\")[0]\n",
|
55 |
+
"\n",
|
56 |
+
" opath = f\"./data/text/{filename}.txt\"\n",
|
57 |
+
"\n",
|
58 |
+
" if os.path.exists(opath):\n",
|
59 |
+
" continue\n",
|
60 |
+
" # pass\n",
|
61 |
+
"\n",
|
62 |
+
" response = requests.get(aozora_url)\n",
|
63 |
+
"\n",
|
64 |
+
" response.encoding = response.apparent_encoding\n",
|
65 |
+
"\n",
|
66 |
+
" soup = BeautifulSoup(response.text, \"html.parser\")\n",
|
67 |
+
"\n",
|
68 |
+
" div = soup.find(\"div\", class_=\"main_text\") \n",
|
69 |
+
"\n",
|
70 |
+
" txt = div.get_text().strip()\n",
|
71 |
+
"\n",
|
72 |
+
" os.makedirs(os.path.dirname(opath), exist_ok=True)\n",
|
73 |
+
"\n",
|
74 |
+
" with open(opath, \"w\") as f:\n",
|
75 |
+
" f.write(txt)"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": null,
|
81 |
+
"metadata": {},
|
82 |
+
"outputs": [],
|
83 |
+
"source": []
|
84 |
+
}
|
85 |
+
],
|
86 |
+
"metadata": {
|
87 |
+
"kernelspec": {
|
88 |
+
"display_name": ".venv",
|
89 |
+
"language": "python",
|
90 |
+
"name": "python3"
|
91 |
+
},
|
92 |
+
"language_info": {
|
93 |
+
"codemirror_mode": {
|
94 |
+
"name": "ipython",
|
95 |
+
"version": 3
|
96 |
+
},
|
97 |
+
"file_extension": ".py",
|
98 |
+
"mimetype": "text/x-python",
|
99 |
+
"name": "python",
|
100 |
+
"nbconvert_exporter": "python",
|
101 |
+
"pygments_lexer": "ipython3",
|
102 |
+
"version": "3.9.11"
|
103 |
+
}
|
104 |
+
},
|
105 |
+
"nbformat": 4,
|
106 |
+
"nbformat_minor": 2
|
107 |
+
}
|
src/02_llm.ipynb
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import os\n",
|
10 |
+
"from llama_index.llms.azure_openai import AzureOpenAI\n",
|
11 |
+
"from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding\n",
|
12 |
+
"from llama_index.core import SimpleDirectoryReader, Settings, VectorStoreIndex"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 16,
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n",
|
22 |
+
"api_version = \"2024-05-01-preview\"\n",
|
23 |
+
"azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\")"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 17,
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"llm = AzureOpenAI(\n",
|
33 |
+
" model=\"gpt-4o\",\n",
|
34 |
+
" deployment_name=\"gpt-4o\",\n",
|
35 |
+
" api_key=api_key,\n",
|
36 |
+
" azure_endpoint=azure_endpoint,\n",
|
37 |
+
" api_version=api_version,\n",
|
38 |
+
")"
|
39 |
+
]
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"cell_type": "code",
|
43 |
+
"execution_count": 18,
|
44 |
+
"metadata": {},
|
45 |
+
"outputs": [],
|
46 |
+
"source": [
|
47 |
+
"# You need to deploy your own embedding model as well as your own chat completion model\n",
|
48 |
+
"embed_model = AzureOpenAIEmbedding(\n",
|
49 |
+
" model=\"text-embedding-3-small\",\n",
|
50 |
+
" deployment_name=\"text-embedding-3-small\",\n",
|
51 |
+
" api_key=api_key,\n",
|
52 |
+
" azure_endpoint=azure_endpoint,\n",
|
53 |
+
" api_version=api_version,\n",
|
54 |
+
")"
|
55 |
+
]
|
56 |
+
},
|
57 |
+
{
|
58 |
+
"cell_type": "code",
|
59 |
+
"execution_count": 19,
|
60 |
+
"metadata": {},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"Settings.llm = llm\n",
|
64 |
+
"Settings.embed_model = embed_model"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"cell_type": "code",
|
69 |
+
"execution_count": 20,
|
70 |
+
"metadata": {},
|
71 |
+
"outputs": [],
|
72 |
+
"source": [
|
73 |
+
"# Data Source -> Documents化を行うStep\n",
|
74 |
+
"documents = SimpleDirectoryReader(\n",
|
75 |
+
" input_dir=\"./data/text\"\n",
|
76 |
+
").load_data()"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": null,
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [],
|
84 |
+
"source": [
|
85 |
+
"index = VectorStoreIndex.from_documents(documents)"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": null,
|
91 |
+
"metadata": {},
|
92 |
+
"outputs": [],
|
93 |
+
"source": [
|
94 |
+
"index.storage_context.persist(persist_dir=\"../index\")"
|
95 |
+
]
|
96 |
+
}
|
97 |
+
],
|
98 |
+
"metadata": {
|
99 |
+
"kernelspec": {
|
100 |
+
"display_name": ".venv",
|
101 |
+
"language": "python",
|
102 |
+
"name": "python3"
|
103 |
+
},
|
104 |
+
"language_info": {
|
105 |
+
"codemirror_mode": {
|
106 |
+
"name": "ipython",
|
107 |
+
"version": 3
|
108 |
+
},
|
109 |
+
"file_extension": ".py",
|
110 |
+
"mimetype": "text/x-python",
|
111 |
+
"name": "python",
|
112 |
+
"nbconvert_exporter": "python",
|
113 |
+
"pygments_lexer": "ipython3",
|
114 |
+
"version": "3.9.11"
|
115 |
+
}
|
116 |
+
},
|
117 |
+
"nbformat": 4,
|
118 |
+
"nbformat_minor": 2
|
119 |
+
}
|