lizhen30 commited on
Commit
91e0a96
·
1 Parent(s): d4e21ec

adb llms cache

Browse files
.langchain.db ADDED
Binary file (12.3 kB). View file
 
README.md CHANGED
@@ -4,16 +4,39 @@ emoji: 🚀
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.24.1
8
  app_file: app.py
9
  pinned: false
10
  license: openrail
11
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
 
13
 
14
- | 日期 |   进度 | 备注 |
15
- | :--: | :--- | :--: |
16
- |   2023/04/14   |   接入openAI,简单测试。| |
17
- | 2023/04/15 | &nbsp;&nbsp;熟悉LangChain API。<br> &nbsp;&nbsp;熟悉Chain的概念。 <br> &nbsp;&nbsp;熟悉agent goole search。<br> &nbsp;&nbsp;熟悉Prompt Template。| |
18
 
19
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 3.24.1ß
8
  app_file: app.py
9
  pinned: false
10
  license: openrail
11
  ---
12
+ Anaconda + VScode 的Python环境搭建
13
+ https://blog.csdn.net/weixin_43876852/article/details/125793075
14
+ Anaconda配置安装
15
+ https://www.anaconda.com/products/distribution/start-coding-immediately
16
+ langchain
17
+ https://pypi.org/project/langchain/
18
+ onnxruntim官网
19
+ https://onnxruntime.ai/
20
+ 解决ERROR: Could not find a version that satisfies the requirement onnxruntime (from versions: none)
21
+ https://blog.csdn.net/ccheiheihei/article/details/125556753
22
+ onnxruntime connect to feed.修改Mac python默认pip源
23
+ https://blog.csdn.net/m0_59550201/article/details/125962316
24
+ https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/ort-nightly/overview/1.15.0.dev20230404004
25
+ Mac安装了conda后,解决Mac终端的命令行前的(base)问题
26
+ https://blog.csdn.net/gudeng007/article/details/125534180
27
+ conda安装,找不到module
28
+ https://www.cnblogs.com/Li-JT/p/15477737.html
29
+ Anaconda的升级、配置及使用
30
+ https://blog.csdn.net/turui/article/details/127063642
31
+ Jupyter Notebooks in VS Code / %%time
32
+ https://code.visualstudio.com/docs/datascience/jupyter-notebooks
33
 
34
+ pip3 install langchain
35
+ pip3 install redis
36
 
37
+ 更新langchain:
38
+ pip3 install --upgrade langchain
39
+ 查看安装列表及版本:
40
+ pip3 list
41
 
42
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
data_map_0.txt ADDED
Binary file (11.4 kB). View file
 
faiss.index ADDED
Binary file (9.33 kB). View file
 
llms_cache.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import langchain
2
+ import time
3
+ from langchain.llms import OpenAI
4
+ from langchain.cache import InMemoryCache
5
+ from langchain.cache import SQLiteCache
6
+ from redis import Redis
7
+ from langchain.cache import RedisCache
8
+
9
+ # InMemoryCache or SQLiteCache or RedisCache
10
+ # use 'rm .langchain.db' to delete db when use SQLiteCache。
11
+ # langchain.llm_cache = InMemoryCache()
12
+ # langchain.llm_cache = SQLiteCache()
13
+ # langchain.llm_cache = RedisCache(redis_=Redis())
14
+ llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)
15
+
16
+ start = time.perf_counter()
17
+ print(llm("今日中国新闻有哪些"))
18
+ print("first suspend:", time.perf_counter() - start)
19
+
20
+ start = time.perf_counter()
21
+ print(llm("今日中国新闻有哪些"))
22
+ print("second suspend:{:0.10f}".format(time.perf_counter() - start))
llms_cache_gpt.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #测试GPTCache,进行精确匹配缓存或基于语义相似性缓存结果
2
+ import langchain
3
+ import gptcache
4
+ import time
5
+ from langchain.llms import OpenAI
6
+ from gptcache.processor.pre import get_prompt
7
+ from gptcache.manager.factory import get_data_manager
8
+ from langchain.cache import GPTCache
9
+
10
+ llm = OpenAI(model_name="text-davinci-003", n=2, best_of=2)
11
+
12
+ # Avoid multiple caches using the same file, causing different llm model caches to affect each other
13
+ i = 0
14
+ file_prefix = "data_map"
15
+
16
+ def init_gptcache_map(cache_obj: gptcache.Cache):
17
+ global i
18
+ cache_path = f'{file_prefix}_{i}.txt'
19
+ cache_obj.init(
20
+ pre_embedding_func=get_prompt,
21
+ data_manager=get_data_manager(data_path=cache_path),
22
+ )
23
+ i += 1
24
+
25
+ langchain.llm_cache = GPTCache(init_gptcache_map)
26
+
27
+ for i in range(20):
28
+ start = time.perf_counter()
29
+ prompt = "男生有2人,女生有{:d}人,一共多少人?".format(i)
30
+ print("男生有2人,女生有{:d}人, {:s}。 suspend: {:0.4f}".format(i, llm(prompt), time.perf_counter() - start))
llms_cache_gpt_similarity.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import langchain
2
+ import gptcache
3
+ import time
4
+ from langchain.llms import OpenAI
5
+ from gptcache.processor.pre import get_prompt
6
+ from gptcache.manager.factory import get_data_manager
7
+ from langchain.cache import GPTCache
8
+ from gptcache.manager import get_data_manager, CacheBase, VectorBase
9
+ from gptcache import Cache
10
+ from gptcache.embedding import Onnx
11
+ from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation
12
+
13
+ llm = OpenAI(model_name="text-davinci-003")
14
+
15
+ # Avoid multiple caches using the same file, causing different llm model caches to affect each other
16
+ i = 0
17
+ file_prefix = "data_map_similarity"
18
+ llm_cache = Cache()
19
+
20
+
21
+ def init_gptcache_map(cache_obj: gptcache.Cache):
22
+ global i
23
+ cache_path = f'{file_prefix}_{i}.txt'
24
+ onnx = Onnx()
25
+ cache_base = CacheBase('sqlite')
26
+ vector_base = VectorBase('faiss', dimension=onnx.dimension)
27
+ data_manager = get_data_manager(cache_base, vector_base, max_size=10, clean_size=2, data_path=cache_path)
28
+ cache_obj.init(
29
+ pre_embedding_func=get_prompt,
30
+ embedding_func=onnx.to_embeddings,
31
+ data_manager=data_manager,
32
+ similarity_evaluation=SearchDistanceEvaluation(),
33
+ )
34
+ i += 1
35
+
36
+ langchain.llm_cache = GPTCache(init_gptcache_map)
37
+
38
+ %%time
39
+ for i in range(20):
40
+ start = time.perf_counter()
41
+ prompt = "男生有2人,女生有{:d}人,一共多少人?".format(i)
42
+ print("男生有2人,女生有{:d}人, {:s}。 suspend: {:0.4f}".format(i, llm(prompt), time.perf_counter() - start))
llms_cache_option.ipynb ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 10,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.llms import OpenAI\n",
10
+ "\n",
11
+ "llm = OpenAI(model_name=\"text-davinci-002\", n=2, best_of=2, cache=False)"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 11,
17
+ "metadata": {},
18
+ "outputs": [
19
+ {
20
+ "name": "stdout",
21
+ "output_type": "stream",
22
+ "text": [
23
+ "CPU times: user 5.07 ms, sys: 2.21 ms, total: 7.28 ms\n",
24
+ "Wall time: 995 ms\n"
25
+ ]
26
+ },
27
+ {
28
+ "data": {
29
+ "text/plain": [
30
+ "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'"
31
+ ]
32
+ },
33
+ "execution_count": 11,
34
+ "metadata": {},
35
+ "output_type": "execute_result"
36
+ }
37
+ ],
38
+ "source": [
39
+ "%%time\n",
40
+ "llm(\"Tell me a joke\")"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 12,
46
+ "metadata": {},
47
+ "outputs": [
48
+ {
49
+ "name": "stdout",
50
+ "output_type": "stream",
51
+ "text": [
52
+ "CPU times: user 5.11 ms, sys: 2.25 ms, total: 7.36 ms\n",
53
+ "Wall time: 818 ms\n"
54
+ ]
55
+ },
56
+ {
57
+ "data": {
58
+ "text/plain": [
59
+ "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.'"
60
+ ]
61
+ },
62
+ "execution_count": 12,
63
+ "metadata": {},
64
+ "output_type": "execute_result"
65
+ }
66
+ ],
67
+ "source": [
68
+ "%%time\n",
69
+ "llm(\"Tell me a joke\")"
70
+ ]
71
+ }
72
+ ],
73
+ "metadata": {
74
+ "kernelspec": {
75
+ "display_name": "base",
76
+ "language": "python",
77
+ "name": "python3"
78
+ },
79
+ "language_info": {
80
+ "codemirror_mode": {
81
+ "name": "ipython",
82
+ "version": 3
83
+ },
84
+ "file_extension": ".py",
85
+ "mimetype": "text/x-python",
86
+ "name": "python",
87
+ "nbconvert_exporter": "python",
88
+ "pygments_lexer": "ipython3",
89
+ "version": "3.10.10"
90
+ },
91
+ "orig_nbformat": 4
92
+ },
93
+ "nbformat": 4,
94
+ "nbformat_minor": 2
95
+ }
llms_cache_option.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from langchain.llms import OpenAI
2
+
3
+ llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2, cache=False)
4
+
5
+ %%time
6
+ llm("Tell me a joke")
llms_cache_option_chain.ipynb ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 10,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.llms import OpenAI\n",
10
+ "\n",
11
+ "llm = OpenAI(model_name=\"text-davinci-002\")\n",
12
+ "no_cache_llm = OpenAI(model_name=\"text-davinci-002\", cache=False)"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 11,
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "from langchain.text_splitter import CharacterTextSplitter\n",
22
+ "from langchain.chains.mapreduce import MapReduceChain\n",
23
+ "\n",
24
+ "text_splitter = CharacterTextSplitter()"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": 12,
30
+ "metadata": {},
31
+ "outputs": [],
32
+ "source": [
33
+ "with open('./txt/poem.txt') as f:\n",
34
+ " state_of_the_union = f.read()\n",
35
+ "texts = text_splitter.split_text(state_of_the_union)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 13,
41
+ "metadata": {},
42
+ "outputs": [],
43
+ "source": [
44
+ "from langchain.docstore.document import Document\n",
45
+ "docs = [Document(page_content=t) for t in texts[:3]]\n",
46
+ "from langchain.chains.summarize import load_summarize_chain"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 14,
52
+ "metadata": {},
53
+ "outputs": [],
54
+ "source": [
55
+ "chain = load_summarize_chain(llm, chain_type=\"map_reduce\", reduce_llm=no_cache_llm)"
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": 15,
61
+ "metadata": {},
62
+ "outputs": [
63
+ {
64
+ "name": "stderr",
65
+ "output_type": "stream",
66
+ "text": [
67
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.\n"
68
+ ]
69
+ },
70
+ {
71
+ "name": "stdout",
72
+ "output_type": "stream",
73
+ "text": [
74
+ "CPU times: user 367 ms, sys: 39.4 ms, total: 406 ms\n",
75
+ "Wall time: 34.3 s\n"
76
+ ]
77
+ },
78
+ {
79
+ "data": {
80
+ "text/plain": [
81
+ "'\\n\\nA young woman in Suzhou is pining for her lover who has left her. She spends her days drinking and looking at the moon, hoping he will return to her.'"
82
+ ]
83
+ },
84
+ "execution_count": 15,
85
+ "metadata": {},
86
+ "output_type": "execute_result"
87
+ }
88
+ ],
89
+ "source": [
90
+ "%%time\n",
91
+ "chain.run(docs)"
92
+ ]
93
+ }
94
+ ],
95
+ "metadata": {
96
+ "kernelspec": {
97
+ "display_name": "base",
98
+ "language": "python",
99
+ "name": "python3"
100
+ },
101
+ "language_info": {
102
+ "codemirror_mode": {
103
+ "name": "ipython",
104
+ "version": 3
105
+ },
106
+ "file_extension": ".py",
107
+ "mimetype": "text/x-python",
108
+ "name": "python",
109
+ "nbconvert_exporter": "python",
110
+ "pygments_lexer": "ipython3",
111
+ "version": "3.10.10"
112
+ },
113
+ "orig_nbformat": 4
114
+ },
115
+ "nbformat": 4,
116
+ "nbformat_minor": 2
117
+ }
llms_fake.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.llms.fake import FakeListLLM
2
+ from langchain.agents import load_tools
3
+ from langchain.agents import initialize_agent
4
+ from langchain.agents import AgentType
5
+
6
+ tools = load_tools(tool_names=["python_repl"])
7
+ responses = [
8
+ "Action: Python REPL\nAction Input: print(2 + 2)",
9
+ "Final Answer: 4"
10
+ ]
11
+ llm = FakeListLLM(responses=responses)
12
+ agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
13
+
14
+ result = agent.run("55+66")
15
+ print("result:",result)
requirements.txt CHANGED
@@ -1 +1 @@
1
- langchain==0.0.139
 
1
+ langchain==0.0.141
sqlite.db ADDED
Binary file (24.6 kB). View file
 
test.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ 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):
9
+ resp = llm.generate(["Hello, how are you?"])
10
+ print(resp.generations[0][0].text)
11
+
12
+
13
+ async def async_generate(llm):
14
+ resp = await llm.agenerate(["Hello, how are you?"])
15
+ print(resp.generations[0][0].text)
16
+
17
+
18
+ async def generate_concurrently():
19
+ llm = OpenAI(temperature=0.9)
20
+ tasks = [async_generate(llm) for _ in range(10)]
21
+ await asyncio.gather(*tasks)
22
+
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' + f"Concurrent executed in {elapsed:0.2f} seconds." + '\033[0m')
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')
txt/poem.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ 落拓江湖常载酒,十年重见云英,依然绰约掌中轻。
2
+ 灯前才一笑,偷解砑罗裙。
3
+ 薄幸萧郎憔悴甚,此身终负卿卿。
4
+ 姑苏城上月黄昏。
5
+ 绿窗人去住,红粉泪纵横。ß