Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
3de6db3
1
Parent(s):
56ac16d
add redis
Browse files- app.py +3 -1
- data/llm_test.csv +3 -1
- index_bilibili.ipynb +19 -13
- llms_asyncio.py +5 -3
- openai_chat_prompt_template.py +5 -3
- redis/test_common.ipynb +123 -0
- redis/test_string.ipynb +111 -0
- test_csv/test_pandas.ipynb +62 -20
app.py
CHANGED
@@ -2,10 +2,12 @@ import gradio as gr
|
|
2 |
from langchain.llms import OpenAI
|
3 |
from langchain.llms import PromptLayerOpenAI
|
4 |
|
|
|
5 |
def chatOpenAI(input):
|
6 |
llm = OpenAI(temperature=0.9)
|
7 |
return llm(input)
|
8 |
|
|
|
9 |
def promptLayer(input):
|
10 |
llm = PromptLayerOpenAI(pl_tags=["LangChainGo"])
|
11 |
return llm(input)
|
@@ -14,4 +16,4 @@ def promptLayer(input):
|
|
14 |
with gr.Blocks() as demo:
|
15 |
gr.Markdown("# LangChain Test,LLM跑步上车。")
|
16 |
gr.Interface(fn=promptLayer, inputs="text", outputs="text")
|
17 |
-
demo.launch()
|
|
|
2 |
from langchain.llms import OpenAI
|
3 |
from langchain.llms import PromptLayerOpenAI
|
4 |
|
5 |
+
|
6 |
def chatOpenAI(input):
|
7 |
llm = OpenAI(temperature=0.9)
|
8 |
return llm(input)
|
9 |
|
10 |
+
|
11 |
def promptLayer(input):
|
12 |
llm = PromptLayerOpenAI(pl_tags=["LangChainGo"])
|
13 |
return llm(input)
|
|
|
16 |
with gr.Blocks() as demo:
|
17 |
gr.Markdown("# LangChain Test,LLM跑步上车。")
|
18 |
gr.Interface(fn=promptLayer, inputs="text", outputs="text")
|
19 |
+
demo.launch()
|
data/llm_test.csv
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
name,age,city,mark
|
2 |
-
张三,
|
3 |
赵四,19,河北,kk
|
4 |
赵五,100,河北,kk
|
5 |
赵六,19,河北,kk
|
6 |
王五,19,河北,kk
|
|
|
|
|
|
1 |
name,age,city,mark
|
2 |
+
张三,212,河北,kk
|
3 |
赵四,19,河北,kk
|
4 |
赵五,100,河北,kk
|
5 |
赵六,19,河北,kk
|
6 |
王五,19,河北,kk
|
7 |
+
,name,age,city,mark
|
8 |
+
0,张三,2345,河北,kk
|
index_bilibili.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
@@ -11,7 +11,7 @@
|
|
11 |
},
|
12 |
{
|
13 |
"cell_type": "code",
|
14 |
-
"execution_count":
|
15 |
"metadata": {},
|
16 |
"outputs": [],
|
17 |
"source": [
|
@@ -20,29 +20,35 @@
|
|
20 |
},
|
21 |
{
|
22 |
"cell_type": "code",
|
23 |
-
"execution_count":
|
24 |
"metadata": {},
|
25 |
"outputs": [],
|
26 |
"source": [
|
27 |
"loader = BiliBiliLoader(\n",
|
28 |
-
" [\"https://www.bilibili.com/video/
|
29 |
")"
|
30 |
]
|
31 |
},
|
32 |
{
|
33 |
"cell_type": "code",
|
34 |
-
"execution_count":
|
35 |
"metadata": {},
|
36 |
"outputs": [
|
37 |
{
|
38 |
-
"
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
}
|
47 |
],
|
48 |
"source": [
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 5,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
|
|
11 |
},
|
12 |
{
|
13 |
"cell_type": "code",
|
14 |
+
"execution_count": 6,
|
15 |
"metadata": {},
|
16 |
"outputs": [],
|
17 |
"source": [
|
|
|
20 |
},
|
21 |
{
|
22 |
"cell_type": "code",
|
23 |
+
"execution_count": 7,
|
24 |
"metadata": {},
|
25 |
"outputs": [],
|
26 |
"source": [
|
27 |
"loader = BiliBiliLoader(\n",
|
28 |
+
" [\"https://www.bilibili.com/video/BV1DP411m7TR/\"]\n",
|
29 |
")"
|
30 |
]
|
31 |
},
|
32 |
{
|
33 |
"cell_type": "code",
|
34 |
+
"execution_count": 8,
|
35 |
"metadata": {},
|
36 |
"outputs": [
|
37 |
{
|
38 |
+
"ename": "RuntimeError",
|
39 |
+
"evalue": "This event loop is already running",
|
40 |
+
"output_type": "error",
|
41 |
+
"traceback": [
|
42 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
43 |
+
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
|
44 |
+
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m loader\u001b[39m.\u001b[39;49mload()\n",
|
45 |
+
"File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/document_loaders/bilibili.py:23\u001b[0m, in \u001b[0;36mBiliBiliLoader.load\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 21\u001b[0m results \u001b[39m=\u001b[39m []\n\u001b[1;32m 22\u001b[0m \u001b[39mfor\u001b[39;00m url \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mvideo_urls:\n\u001b[0;32m---> 23\u001b[0m transcript, video_info \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_bilibili_subs_and_info(url)\n\u001b[1;32m 24\u001b[0m doc \u001b[39m=\u001b[39m Document(page_content\u001b[39m=\u001b[39mtranscript, metadata\u001b[39m=\u001b[39mvideo_info)\n\u001b[1;32m 25\u001b[0m results\u001b[39m.\u001b[39mappend(doc)\n",
|
46 |
+
"File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/document_loaders/bilibili.py:51\u001b[0m, in \u001b[0;36mBiliBiliLoader._get_bilibili_subs_and_info\u001b[0;34m(self, url)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 49\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00murl\u001b[39m}\u001b[39;00m\u001b[39m is not bilibili url.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m---> 51\u001b[0m video_info \u001b[39m=\u001b[39m sync(v\u001b[39m.\u001b[39;49mget_info())\n\u001b[1;32m 52\u001b[0m video_info\u001b[39m.\u001b[39mupdate({\u001b[39m\"\u001b[39m\u001b[39murl\u001b[39m\u001b[39m\"\u001b[39m: url})\n\u001b[1;32m 54\u001b[0m \u001b[39m# Get subtitle url\u001b[39;00m\n",
|
47 |
+
"File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/bilibili_api/utils/sync.py:24\u001b[0m, in \u001b[0;36msync\u001b[0;34m(coroutine)\u001b[0m\n\u001b[1;32m 22\u001b[0m __ensure_event_loop()\n\u001b[1;32m 23\u001b[0m loop \u001b[39m=\u001b[39m asyncio\u001b[39m.\u001b[39mget_event_loop()\n\u001b[0;32m---> 24\u001b[0m \u001b[39mreturn\u001b[39;00m loop\u001b[39m.\u001b[39;49mrun_until_complete(coroutine)\n",
|
48 |
+
"File \u001b[0;32m~/anaconda3/lib/python3.10/asyncio/base_events.py:625\u001b[0m, in \u001b[0;36mBaseEventLoop.run_until_complete\u001b[0;34m(self, future)\u001b[0m\n\u001b[1;32m 614\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Run until the Future is done.\u001b[39;00m\n\u001b[1;32m 615\u001b[0m \n\u001b[1;32m 616\u001b[0m \u001b[39mIf the argument is a coroutine, it is wrapped in a Task.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 622\u001b[0m \u001b[39mReturn the Future's result, or raise its exception.\u001b[39;00m\n\u001b[1;32m 623\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 624\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_check_closed()\n\u001b[0;32m--> 625\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_check_running()\n\u001b[1;32m 627\u001b[0m new_task \u001b[39m=\u001b[39m \u001b[39mnot\u001b[39;00m futures\u001b[39m.\u001b[39misfuture(future)\n\u001b[1;32m 628\u001b[0m future \u001b[39m=\u001b[39m tasks\u001b[39m.\u001b[39mensure_future(future, loop\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m)\n",
|
49 |
+
"File \u001b[0;32m~/anaconda3/lib/python3.10/asyncio/base_events.py:584\u001b[0m, in \u001b[0;36mBaseEventLoop._check_running\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 582\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_check_running\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[1;32m 583\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mis_running():\n\u001b[0;32m--> 584\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mRuntimeError\u001b[39;00m(\u001b[39m'\u001b[39m\u001b[39mThis event loop is already running\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m 585\u001b[0m \u001b[39mif\u001b[39;00m events\u001b[39m.\u001b[39m_get_running_loop() \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 586\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mRuntimeError\u001b[39;00m(\n\u001b[1;32m 587\u001b[0m \u001b[39m'\u001b[39m\u001b[39mCannot run the event loop while another loop is running\u001b[39m\u001b[39m'\u001b[39m)\n",
|
50 |
+
"\u001b[0;31mRuntimeError\u001b[0m: This event loop is already running"
|
51 |
+
]
|
52 |
}
|
53 |
],
|
54 |
"source": [
|
llms_asyncio.py
CHANGED
@@ -3,6 +3,7 @@ import asyncio
|
|
3 |
|
4 |
from langchain.llms import OpenAI
|
5 |
|
|
|
6 |
def generate_serially():
|
7 |
llm = OpenAI(temperature=0.9)
|
8 |
for _ in range(10):
|
@@ -23,11 +24,12 @@ async def generate_concurrently():
|
|
23 |
|
24 |
s = time.perf_counter()
|
25 |
# If running this outside of Jupyter, use asyncio.run(generate_concurrently())
|
26 |
-
generate_concurrently()
|
27 |
elapsed = time.perf_counter() - s
|
28 |
-
print('\033[1m' +
|
|
|
29 |
|
30 |
s = time.perf_counter()
|
31 |
generate_serially()
|
32 |
elapsed = time.perf_counter() - s
|
33 |
-
print('\033[1m' + f"Serial executed in {elapsed:0.2f} seconds." + '\033[0m')
|
|
|
3 |
|
4 |
from langchain.llms import OpenAI
|
5 |
|
6 |
+
|
7 |
def generate_serially():
|
8 |
llm = OpenAI(temperature=0.9)
|
9 |
for _ in range(10):
|
|
|
24 |
|
25 |
s = time.perf_counter()
|
26 |
# If running this outside of Jupyter, use asyncio.run(generate_concurrently())
|
27 |
+
generate_concurrently()
|
28 |
elapsed = time.perf_counter() - s
|
29 |
+
print('\033[1m' +
|
30 |
+
f"Concurrent executed in {elapsed:0.2f} seconds." + '\033[0m')
|
31 |
|
32 |
s = time.perf_counter()
|
33 |
generate_serially()
|
34 |
elapsed = time.perf_counter() - s
|
35 |
+
print('\033[1m' + f"Serial executed in {elapsed:0.2f} seconds." + '\033[0m')
|
openai_chat_prompt_template.py
CHANGED
@@ -15,11 +15,13 @@ human_template = "{text}"
|
|
15 |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
16 |
|
17 |
# 这里是使用chat请求,返回BaseMessage。
|
18 |
-
chat_prompt = ChatPromptTemplate.from_messages(
|
19 |
-
|
|
|
|
|
20 |
print(result.content)
|
21 |
|
22 |
# 这里是使用chain请求,返回str, 带有聊天模型的chain。
|
23 |
chain = LLMChain(llm=chat, prompt=chat_prompt)
|
24 |
result = chain.run(input_language="中文", output_language="英语", text="我想请假")
|
25 |
-
print(result)
|
|
|
15 |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
16 |
|
17 |
# 这里是使用chat请求,返回BaseMessage。
|
18 |
+
chat_prompt = ChatPromptTemplate.from_messages(
|
19 |
+
[system_message_prompt, human_message_prompt])
|
20 |
+
result = chat(chat_prompt.format_prompt(input_language="中文",
|
21 |
+
output_language="英语", text="我想请假").to_messages())
|
22 |
print(result.content)
|
23 |
|
24 |
# 这里是使用chain请求,返回str, 带有聊天模型的chain。
|
25 |
chain = LLMChain(llm=chat, prompt=chat_prompt)
|
26 |
result = chain.run(input_language="中文", output_language="英语", text="我想请假")
|
27 |
+
print(result)
|
redis/test_common.ipynb
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 13,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import redis\n",
|
10 |
+
"\n",
|
11 |
+
"conn_pool = redis.ConnectionPool(host='10.254.13.87', port='6379', max_connections=10)"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 14,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [
|
19 |
+
{
|
20 |
+
"name": "stdout",
|
21 |
+
"output_type": "stream",
|
22 |
+
"text": [
|
23 |
+
"[b'user', b'test_hello', b'www.biancheng.net', b'test_list', b'test_hash', b'test_set', b'fans:num', b'user:id:01:username']\n"
|
24 |
+
]
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"source": [
|
28 |
+
"r = redis.Redis(connection_pool=conn_pool)\n",
|
29 |
+
"key_list = r.keys('*')\n",
|
30 |
+
"print(key_list)"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "code",
|
35 |
+
"execution_count": 15,
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [
|
38 |
+
{
|
39 |
+
"name": "stdout",
|
40 |
+
"output_type": "stream",
|
41 |
+
"text": [
|
42 |
+
"user b'hash'\n",
|
43 |
+
"test_hello b'string'\n",
|
44 |
+
"www.biancheng.net b'string'\n",
|
45 |
+
"test_list b'list'\n",
|
46 |
+
"test_hash b'hash'\n",
|
47 |
+
"test_set b'set'\n",
|
48 |
+
"fans:num b'string'\n",
|
49 |
+
"user:id:01:username b'string'\n"
|
50 |
+
]
|
51 |
+
}
|
52 |
+
],
|
53 |
+
"source": [
|
54 |
+
"for key in key_list:\n",
|
55 |
+
" keyStr = key.decode()\n",
|
56 |
+
" print(keyStr, r.type(keyStr))"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 19,
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [
|
64 |
+
{
|
65 |
+
"name": "stdout",
|
66 |
+
"output_type": "stream",
|
67 |
+
"text": [
|
68 |
+
"b'hash'\n",
|
69 |
+
"1\n",
|
70 |
+
"0\n"
|
71 |
+
]
|
72 |
+
}
|
73 |
+
],
|
74 |
+
"source": [
|
75 |
+
"print(r.type('user'))\n",
|
76 |
+
"print(r.exists('test_hello'))\n",
|
77 |
+
"print(r.exists('user1'))"
|
78 |
+
]
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"cell_type": "code",
|
82 |
+
"execution_count": 24,
|
83 |
+
"metadata": {},
|
84 |
+
"outputs": [
|
85 |
+
{
|
86 |
+
"name": "stdout",
|
87 |
+
"output_type": "stream",
|
88 |
+
"text": [
|
89 |
+
"0\n",
|
90 |
+
"0\n"
|
91 |
+
]
|
92 |
+
}
|
93 |
+
],
|
94 |
+
"source": [
|
95 |
+
"delete_result = r.delete('test_hello', 'test_hell2')\n",
|
96 |
+
"print(delete_result)\n",
|
97 |
+
"print(r.exists('test_hello'))"
|
98 |
+
]
|
99 |
+
}
|
100 |
+
],
|
101 |
+
"metadata": {
|
102 |
+
"kernelspec": {
|
103 |
+
"display_name": "base",
|
104 |
+
"language": "python",
|
105 |
+
"name": "python3"
|
106 |
+
},
|
107 |
+
"language_info": {
|
108 |
+
"codemirror_mode": {
|
109 |
+
"name": "ipython",
|
110 |
+
"version": 3
|
111 |
+
},
|
112 |
+
"file_extension": ".py",
|
113 |
+
"mimetype": "text/x-python",
|
114 |
+
"name": "python",
|
115 |
+
"nbconvert_exporter": "python",
|
116 |
+
"pygments_lexer": "ipython3",
|
117 |
+
"version": "3.10.10"
|
118 |
+
},
|
119 |
+
"orig_nbformat": 4
|
120 |
+
},
|
121 |
+
"nbformat": 4,
|
122 |
+
"nbformat_minor": 2
|
123 |
+
}
|
redis/test_string.ipynb
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import redis\n",
|
10 |
+
"\n",
|
11 |
+
"r = redis.Redis(host='10.254.13.87', port='6379', db=0)"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 2,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [
|
19 |
+
{
|
20 |
+
"name": "stdout",
|
21 |
+
"output_type": "stream",
|
22 |
+
"text": [
|
23 |
+
"b'math'\n"
|
24 |
+
]
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"source": [
|
28 |
+
"r.set('subject', 'math')\n",
|
29 |
+
"print(r.get('subject'))"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": 3,
|
35 |
+
"metadata": {},
|
36 |
+
"outputs": [
|
37 |
+
{
|
38 |
+
"name": "stdout",
|
39 |
+
"output_type": "stream",
|
40 |
+
"text": [
|
41 |
+
"[b'lizhen', b'123']\n",
|
42 |
+
"6\n"
|
43 |
+
]
|
44 |
+
}
|
45 |
+
],
|
46 |
+
"source": [
|
47 |
+
"r.mset({'username': 'lizhen', 'password': '123'})\n",
|
48 |
+
"print(r.mget('username', 'password'))\n",
|
49 |
+
"print(r.strlen('username'))"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": 7,
|
55 |
+
"metadata": {},
|
56 |
+
"outputs": [
|
57 |
+
{
|
58 |
+
"name": "stdout",
|
59 |
+
"output_type": "stream",
|
60 |
+
"text": [
|
61 |
+
"b'18'\n",
|
62 |
+
"b'20'\n",
|
63 |
+
"b'17'\n",
|
64 |
+
"b'18'\n",
|
65 |
+
"b'17'\n",
|
66 |
+
"19.19999999999999929\n",
|
67 |
+
"9.19999999999999929\n"
|
68 |
+
]
|
69 |
+
}
|
70 |
+
],
|
71 |
+
"source": [
|
72 |
+
"r.set('age','18')\n",
|
73 |
+
"print(r.get('age'))\n",
|
74 |
+
"r.incrby('age', 2)\n",
|
75 |
+
"print(r.get('age'))\n",
|
76 |
+
"r.decrby('age', 3)\n",
|
77 |
+
"print(r.get('age'))\n",
|
78 |
+
"r.incr('age')\n",
|
79 |
+
"print(r.get('age'))\n",
|
80 |
+
"r.decr('age')\n",
|
81 |
+
"print(r.get('age'))\n",
|
82 |
+
"r.incrbyfloat('age', 2.2)\n",
|
83 |
+
"print(r.get('age').decode())\n",
|
84 |
+
"r.incrbyfloat('age', -10)\n",
|
85 |
+
"print(r.get('age').decode())"
|
86 |
+
]
|
87 |
+
}
|
88 |
+
],
|
89 |
+
"metadata": {
|
90 |
+
"kernelspec": {
|
91 |
+
"display_name": "base",
|
92 |
+
"language": "python",
|
93 |
+
"name": "python3"
|
94 |
+
},
|
95 |
+
"language_info": {
|
96 |
+
"codemirror_mode": {
|
97 |
+
"name": "ipython",
|
98 |
+
"version": 3
|
99 |
+
},
|
100 |
+
"file_extension": ".py",
|
101 |
+
"mimetype": "text/x-python",
|
102 |
+
"name": "python",
|
103 |
+
"nbconvert_exporter": "python",
|
104 |
+
"pygments_lexer": "ipython3",
|
105 |
+
"version": "3.10.10"
|
106 |
+
},
|
107 |
+
"orig_nbformat": 4
|
108 |
+
},
|
109 |
+
"nbformat": 4,
|
110 |
+
"nbformat_minor": 2
|
111 |
+
}
|
test_csv/test_pandas.ipynb
CHANGED
@@ -2,55 +2,97 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
9 |
-
"
|
10 |
-
"from langchain.docstore.document import Document"
|
11 |
]
|
12 |
},
|
13 |
{
|
14 |
"cell_type": "code",
|
15 |
-
"execution_count":
|
16 |
"metadata": {},
|
17 |
"outputs": [],
|
18 |
"source": [
|
19 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"\n",
|
21 |
-
"
|
|
|
22 |
]
|
23 |
},
|
24 |
{
|
25 |
"cell_type": "code",
|
26 |
"execution_count": 6,
|
27 |
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"outputs": [
|
29 |
{
|
30 |
-
"
|
31 |
-
"
|
32 |
-
"
|
33 |
-
|
34 |
-
"\
|
35 |
-
"\
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"\u001b[0;31mTypeError\u001b[0m: __init__() takes exactly 1 positional argument (3 given)"
|
39 |
]
|
40 |
}
|
41 |
],
|
42 |
"source": [
|
43 |
-
"
|
44 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
]
|
46 |
},
|
47 |
{
|
48 |
"cell_type": "code",
|
49 |
-
"execution_count":
|
50 |
"metadata": {},
|
51 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
"source": [
|
53 |
-
"
|
|
|
54 |
]
|
55 |
}
|
56 |
],
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
9 |
+
"import pandas as pd"
|
|
|
10 |
]
|
11 |
},
|
12 |
{
|
13 |
"cell_type": "code",
|
14 |
+
"execution_count": 7,
|
15 |
"metadata": {},
|
16 |
"outputs": [],
|
17 |
"source": [
|
18 |
+
"# 定义要写入的数据\n",
|
19 |
+
"data = [\n",
|
20 |
+
" ['王五', '19', '河北', 'kk']\n",
|
21 |
+
"]\n",
|
22 |
+
"\n",
|
23 |
+
"# 创建DataFrame对象\n",
|
24 |
+
"df = pd.DataFrame(data, columns=['name', 'age', 'city', 'mark'])\n",
|
25 |
"\n",
|
26 |
+
"# 将DataFrame写入CSV文件\n",
|
27 |
+
"df.to_csv('../data/llm_test.csv', index=False, mode='a', header=False)\n"
|
28 |
]
|
29 |
},
|
30 |
{
|
31 |
"cell_type": "code",
|
32 |
"execution_count": 6,
|
33 |
"metadata": {},
|
34 |
+
"outputs": [],
|
35 |
+
"source": [
|
36 |
+
"# 读取CSV文件到DataFrame\n",
|
37 |
+
"df = pd.read_csv('../data/llm_test.csv')\n",
|
38 |
+
"# 修改特定行列的数据\n",
|
39 |
+
"row_index = 0 # 要修改的行索引,除去表头,从0开始\n",
|
40 |
+
"column_name = 'age' # 要修改的列名\n",
|
41 |
+
"new_value = '2345' # 修改后的值\n",
|
42 |
+
"\n",
|
43 |
+
"df.loc[row_index, column_name] = new_value\n",
|
44 |
+
"# 将修改后的DataFrame写回到CSV文件\n",
|
45 |
+
"df.to_csv('../data/llm_test.csv', index=True)"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 20,
|
51 |
+
"metadata": {},
|
52 |
"outputs": [
|
53 |
{
|
54 |
+
"name": "stdout",
|
55 |
+
"output_type": "stream",
|
56 |
+
"text": [
|
57 |
+
"name 2\n",
|
58 |
+
"age 2\n",
|
59 |
+
"city 2\n",
|
60 |
+
"mark 2\n",
|
61 |
+
"dtype: int64\n"
|
|
|
62 |
]
|
63 |
}
|
64 |
],
|
65 |
"source": [
|
66 |
+
"# 读取CSV文件到DataFrame\n",
|
67 |
+
"df = pd.read_csv('../data/llm_test.csv')\n",
|
68 |
+
"\n",
|
69 |
+
"# 查找满足特定条件的行数据\n",
|
70 |
+
"condition = df['age'] == 100 # 以Column1列为例,查找值为'Value1'的行\n",
|
71 |
+
"filtered_data = df[condition]\n",
|
72 |
+
"\n",
|
73 |
+
"# 打印查找结果\n",
|
74 |
+
"print(filtered_data)\n"
|
75 |
]
|
76 |
},
|
77 |
{
|
78 |
"cell_type": "code",
|
79 |
+
"execution_count": 4,
|
80 |
"metadata": {},
|
81 |
+
"outputs": [
|
82 |
+
{
|
83 |
+
"name": "stdout",
|
84 |
+
"output_type": "stream",
|
85 |
+
"text": [
|
86 |
+
" name age city mark\n",
|
87 |
+
"0 张三 100 河北 kk\n",
|
88 |
+
"1 赵四 19 河北 kk\n",
|
89 |
+
"2 赵五 100 河北 kk\n"
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
"source": [
|
94 |
+
"df = pd.read_csv('../data/llm_test.csv').head(3)\n",
|
95 |
+
"print(df)"
|
96 |
]
|
97 |
}
|
98 |
],
|