Arcadia822 commited on
Commit
ea3c0cf
·
unverified ·
1 Parent(s): 532a759

feat: :sparkles: Support Hugging Face (#12)

Browse files
README.md CHANGED
@@ -1,3 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Edu Assistant
2
 
3
  [![Checkstyle](https://github.com/Arcadia822/codedog/actions/workflows/flake8.yml/badge.svg)](https://github.com/codedog-ai/edu-assistant/actions/workflows/flake8.yml)
 
1
+ ---
2
+ title: Edu Assistant
3
+ emoji: 🐠
4
+ colorFrom: pink
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.37.0
8
+ app_file: webui/ui.py
9
+ fullWidth: true
10
+ pinned: false
11
+ license: mit
12
+ ---
13
+
14
  # Edu Assistant
15
 
16
  [![Checkstyle](https://github.com/Arcadia822/codedog/actions/workflows/flake8.yml/badge.svg)](https://github.com/codedog-ai/edu-assistant/actions/workflows/flake8.yml)
edu_assistant/learning_tasks/qa.py CHANGED
@@ -122,7 +122,9 @@ class QaTask(BaseTask):
122
  if not self._knowledge:
123
  return ConversationChain(
124
  llm=load_llm(),
125
- memory=ConversationBufferMemory(memory_key=QaTask.HISTORY_KEY, output_key=self._output_key),
 
 
126
  prompt=self._chat_prompt,
127
  )
128
  else:
@@ -132,7 +134,9 @@ class QaTask(BaseTask):
132
  condense_question_llm=load_llm(),
133
  return_source_documents=True,
134
  combine_docs_chain_kwargs={"prompt": self._chat_prompt},
135
- memory=ConversationBufferMemory(memory_key=QaTask.HISTORY_KEY, output_key=self._output_key),
 
 
136
  )
137
 
138
  def ask(
 
122
  if not self._knowledge:
123
  return ConversationChain(
124
  llm=load_llm(),
125
+ memory=ConversationBufferMemory(
126
+ memory_key=QaTask.HISTORY_KEY, output_key=self._output_key, return_messages=True
127
+ ),
128
  prompt=self._chat_prompt,
129
  )
130
  else:
 
134
  condense_question_llm=load_llm(),
135
  return_source_documents=True,
136
  combine_docs_chain_kwargs={"prompt": self._chat_prompt},
137
+ memory=ConversationBufferMemory(
138
+ memory_key=QaTask.HISTORY_KEY, output_key=self._output_key, return_messages=True
139
+ ),
140
  )
141
 
142
  def ask(
examples/qa.py CHANGED
@@ -1,6 +1,8 @@
1
  import json
2
  import logging
3
 
 
 
4
  from edu_assistant.learning_tasks import QaTask
5
  from edu_assistant.utils.common_utils import init_local_logging
6
 
@@ -15,14 +17,14 @@ task = QaTask(instruction=instruction)
15
 
16
  def qa_once():
17
  result = task.ask("请问什么是指针?", session=False)
18
- print(json.dumps(result, ensure_ascii=False, indent=4))
19
 
20
 
21
  def qa_twice():
22
  task = QaTask()
23
  result = task.ask("请问如何释放一个数组?")
24
  result = task.ask("那指针呢?", session_id=result["session_id"])
25
- print(json.dumps(result, ensure_ascii=False, indent=4))
26
 
27
 
28
  if __name__ == "__main__":
 
1
  import json
2
  import logging
3
 
4
+ from fastapi.encoders import jsonable_encoder
5
+
6
  from edu_assistant.learning_tasks import QaTask
7
  from edu_assistant.utils.common_utils import init_local_logging
8
 
 
17
 
18
  def qa_once():
19
  result = task.ask("请问什么是指针?", session=False)
20
+ print(json.dumps(jsonable_encoder(result), ensure_ascii=False, indent=4))
21
 
22
 
23
  def qa_twice():
24
  task = QaTask()
25
  result = task.ask("请问如何释放一个数组?")
26
  result = task.ask("那指针呢?", session_id=result["session_id"])
27
+ print(json.dumps(jsonable_encoder(result), ensure_ascii=False, indent=4))
28
 
29
 
30
  if __name__ == "__main__":
examples/qdrant_vs.py CHANGED
@@ -33,7 +33,7 @@ def add_docs(path: str, collection_name: str):
33
  vs = load_vectorstore(collection_name=collection_name)
34
  loader = DirectoryLoader(path=path, glob="*.txt", loader_cls=TextLoader)
35
  documents = loader.load()
36
- text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
37
  docs = text_splitter.split_documents(documents)
38
 
39
  for doc in docs:
@@ -43,7 +43,7 @@ def add_docs(path: str, collection_name: str):
43
  def qa(collection_name: str, question: str):
44
  chain = RetrievalQA.from_llm(
45
  llm=load_llm(),
46
- retriever=load_vectorstore(collection_name=collection_name).as_retriever(),
47
  return_source_documents=True,
48
  )
49
  result = chain(question)
@@ -53,9 +53,9 @@ def qa(collection_name: str, question: str):
53
  if __name__ == "__main__":
54
  name = "example"
55
  path = "examples/docs"
56
- question = "C和C++的区别是什么?"
57
 
58
- create_collection(name)
59
- add_docs(path, name)
 
60
  qa(name, question)
61
- delete_collection(name)
 
33
  vs = load_vectorstore(collection_name=collection_name)
34
  loader = DirectoryLoader(path=path, glob="*.txt", loader_cls=TextLoader)
35
  documents = loader.load()
36
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=50)
37
  docs = text_splitter.split_documents(documents)
38
 
39
  for doc in docs:
 
43
  def qa(collection_name: str, question: str):
44
  chain = RetrievalQA.from_llm(
45
  llm=load_llm(),
46
+ retriever=load_vectorstore(collection_name=collection_name).as_retriever(k=1),
47
  return_source_documents=True,
48
  )
49
  result = chain(question)
 
53
  if __name__ == "__main__":
54
  name = "example"
55
  path = "examples/docs"
56
+ question = "C++有哪些数据类型修饰符?"
57
 
58
+ # delete_collection(name)
59
+ # create_collection(name)
60
+ # add_docs(path, name)
61
  qa(name, question)
 
requirements.txt ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --trusted-host mirrors.aliyun.com
2
+ --extra-index-url http://mirrors.aliyun.com/pypi/simple
3
+
4
+ aiofiles==23.1.0 ; python_version >= "3.10" and python_version < "3.12"
5
+ aiohttp==3.8.4 ; python_version >= "3.10" and python_version < "3.12"
6
+ aiosignal==1.3.1 ; python_version >= "3.10" and python_version < "3.12"
7
+ altair==5.0.1 ; python_version >= "3.10" and python_version < "3.12"
8
+ anyio==3.7.1 ; python_version >= "3.10" and python_version < "3.12"
9
+ async-timeout==4.0.2 ; python_version >= "3.10" and python_version < "3.12"
10
+ attrs==23.1.0 ; python_version >= "3.10" and python_version < "3.12"
11
+ certifi==2023.5.7 ; python_version >= "3.10" and python_version < "3.12"
12
+ charset-normalizer==3.2.0 ; python_version >= "3.10" and python_version < "3.12"
13
+ click==8.1.5 ; python_version >= "3.10" and python_version < "3.12"
14
+ colorama==0.4.6 ; python_version >= "3.10" and python_version < "3.12" and platform_system == "Windows"
15
+ contourpy==1.1.0 ; python_version >= "3.10" and python_version < "3.12"
16
+ cycler==0.11.0 ; python_version >= "3.10" and python_version < "3.12"
17
+ dataclasses-json==0.5.9 ; python_version >= "3.10" and python_version < "3.12"
18
+ exceptiongroup==1.1.2 ; python_version >= "3.10" and python_version < "3.11"
19
+ fastapi==0.95.2 ; python_version >= "3.10" and python_version < "3.12"
20
+ ffmpy==0.3.1 ; python_version >= "3.10" and python_version < "3.12"
21
+ filelock==3.12.2 ; python_version >= "3.10" and python_version < "3.12"
22
+ fonttools==4.41.0 ; python_version >= "3.10" and python_version < "3.12"
23
+ frozenlist==1.4.0 ; python_version >= "3.10" and python_version < "3.12"
24
+ fsspec==2023.6.0 ; python_version >= "3.10" and python_version < "3.12"
25
+ gradio-client==0.2.10 ; python_version >= "3.10" and python_version < "3.12"
26
+ gradio==3.37.0 ; python_version >= "3.10" and python_version < "3.12"
27
+ greenlet==2.0.2 ; python_version >= "3.10" and python_version < "3.12" and (platform_machine == "win32" or platform_machine == "WIN32" or platform_machine == "AMD64" or platform_machine == "amd64" or platform_machine == "x86_64" or platform_machine == "ppc64le" or platform_machine == "aarch64")
28
+ grpcio-tools==1.56.0 ; python_version >= "3.10" and python_version < "3.12"
29
+ grpcio==1.56.0 ; python_version >= "3.10" and python_version < "3.12"
30
+ h11==0.14.0 ; python_version >= "3.10" and python_version < "3.12"
31
+ h2==4.1.0 ; python_version >= "3.10" and python_version < "3.12"
32
+ hpack==4.0.0 ; python_version >= "3.10" and python_version < "3.12"
33
+ httpcore==0.17.3 ; python_version >= "3.10" and python_version < "3.12"
34
+ httpx==0.24.1 ; python_version >= "3.10" and python_version < "3.12"
35
+ httpx[http2]==0.24.1 ; python_version >= "3.10" and python_version < "3.12"
36
+ huggingface-hub==0.16.4 ; python_version >= "3.10" and python_version < "3.12"
37
+ hyperframe==6.0.1 ; python_version >= "3.10" and python_version < "3.12"
38
+ idna==3.4 ; python_version >= "3.10" and python_version < "3.12"
39
+ jinja2==3.1.2 ; python_version >= "3.10" and python_version < "3.12"
40
+ jsonschema-specifications==2023.6.1 ; python_version >= "3.10" and python_version < "3.12"
41
+ jsonschema==4.18.3 ; python_version >= "3.10" and python_version < "3.12"
42
+ kiwisolver==1.4.4 ; python_version >= "3.10" and python_version < "3.12"
43
+ langchain==0.0.234 ; python_version >= "3.10" and python_version < "3.12"
44
+ langsmith==0.0.5 ; python_version >= "3.10" and python_version < "3.12"
45
+ linkify-it-py==2.0.2 ; python_version >= "3.10" and python_version < "3.12"
46
+ markdown-it-py==2.2.0 ; python_version >= "3.10" and python_version < "3.12"
47
+ markdown-it-py[linkify]==2.2.0 ; python_version >= "3.10" and python_version < "3.12"
48
+ markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "3.12"
49
+ marshmallow-enum==1.5.1 ; python_version >= "3.10" and python_version < "3.12"
50
+ marshmallow==3.19.0 ; python_version >= "3.10" and python_version < "3.12"
51
+ matplotlib==3.7.2 ; python_version >= "3.10" and python_version < "3.12"
52
+ mdit-py-plugins==0.3.3 ; python_version >= "3.10" and python_version < "3.12"
53
+ mdurl==0.1.2 ; python_version >= "3.10" and python_version < "3.12"
54
+ multidict==6.0.4 ; python_version >= "3.10" and python_version < "3.12"
55
+ mypy-extensions==1.0.0 ; python_version >= "3.10" and python_version < "3.12"
56
+ numexpr==2.8.4 ; python_version >= "3.10" and python_version < "3.12"
57
+ numpy==1.25.1 ; python_version >= "3.10" and python_version < "3.12"
58
+ openai==0.27.8 ; python_version >= "3.10" and python_version < "3.12"
59
+ openapi-schema-pydantic==1.2.4 ; python_version >= "3.10" and python_version < "3.12"
60
+ orjson==3.9.2 ; python_version >= "3.10" and python_version < "3.12"
61
+ packaging==23.1 ; python_version >= "3.10" and python_version < "3.12"
62
+ pandas==2.0.3 ; python_version >= "3.10" and python_version < "3.12"
63
+ pillow==10.0.0 ; python_version >= "3.10" and python_version < "3.12"
64
+ portalocker==2.7.0 ; python_version >= "3.10" and python_version < "3.12"
65
+ protobuf==4.23.4 ; python_version >= "3.10" and python_version < "3.12"
66
+ pydantic==1.10.11 ; python_version >= "3.10" and python_version < "3.12"
67
+ pydub==0.25.1 ; python_version >= "3.10" and python_version < "3.12"
68
+ pyparsing==3.0.9 ; python_version >= "3.10" and python_version < "3.12"
69
+ python-dateutil==2.8.2 ; python_version >= "3.10" and python_version < "3.12"
70
+ python-multipart==0.0.6 ; python_version >= "3.10" and python_version < "3.12"
71
+ pytz==2023.3 ; python_version >= "3.10" and python_version < "3.12"
72
+ pywin32==306 ; python_version >= "3.10" and python_version < "3.12" and platform_system == "Windows"
73
+ pyyaml==6.0 ; python_version >= "3.10" and python_version < "3.12"
74
+ qdrant-client==1.3.1 ; python_version >= "3.10" and python_version < "3.12"
75
+ referencing==0.29.1 ; python_version >= "3.10" and python_version < "3.12"
76
+ regex==2023.6.3 ; python_version >= "3.10" and python_version < "3.12"
77
+ requests==2.31.0 ; python_version >= "3.10" and python_version < "3.12"
78
+ rpds-py==0.8.11 ; python_version >= "3.10" and python_version < "3.12"
79
+ semantic-version==2.10.0 ; python_version >= "3.10" and python_version < "3.12"
80
+ setuptools==68.0.0 ; python_version >= "3.10" and python_version < "3.12"
81
+ six==1.16.0 ; python_version >= "3.10" and python_version < "3.12"
82
+ sniffio==1.3.0 ; python_version >= "3.10" and python_version < "3.12"
83
+ sqlalchemy==2.0.18 ; python_version >= "3.10" and python_version < "3.12"
84
+ starlette==0.27.0 ; python_version >= "3.10" and python_version < "3.12"
85
+ tenacity==8.2.2 ; python_version >= "3.10" and python_version < "3.12"
86
+ tiktoken==0.4.0 ; python_version >= "3.10" and python_version < "3.12"
87
+ toolz==0.12.0 ; python_version >= "3.10" and python_version < "3.12"
88
+ tqdm==4.65.0 ; python_version >= "3.10" and python_version < "3.12"
89
+ typing-extensions==4.5.0 ; python_version >= "3.10" and python_version < "3.12"
90
+ typing-inspect==0.9.0 ; python_version >= "3.10" and python_version < "3.12"
91
+ tzdata==2023.3 ; python_version >= "3.10" and python_version < "3.12"
92
+ uc-micro-py==1.0.2 ; python_version >= "3.10" and python_version < "3.12"
93
+ urllib3==1.26.16 ; python_version >= "3.10" and python_version < "3.12"
94
+ uvicorn==0.21.1 ; python_version >= "3.10" and python_version < "3.12"
95
+ websockets==11.0.3 ; python_version >= "3.10" and python_version < "3.12"
96
+ yarl==1.9.2 ; python_version >= "3.10" and python_version < "3.12"
webui/qa.py CHANGED
@@ -59,5 +59,5 @@ with gr.Blocks() as qa_ui:
59
  with gr.Column(scale=1):
60
  apply = gr.Button(value="更换Prompt")
61
 
62
- msg.submit(respond, [msg, chatbot, session_id], [msg, chatbot, session_id, telemetry])
63
  apply.click(recreate, [instruction], [])
 
59
  with gr.Column(scale=1):
60
  apply = gr.Button(value="更换Prompt")
61
 
62
+ msg.submit(respond, [msg, chatbot, session_id], [msg, chatbot, session_id, telemetry, docs])
63
  apply.click(recreate, [instruction], [])