xuyingliKepler commited on
Commit
4c9dd05
β€’
1 Parent(s): 369f056

Upload 6 files

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. 1.png +3 -0
  3. 2.png +0 -0
  4. 3.png +0 -0
  5. README.md +2 -12
  6. requirements.txt +10 -0
  7. streamlit_test_catch.py +198 -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
+ 1.png filter=lfs diff=lfs merge=lfs -text
1.png ADDED

Git LFS Details

  • SHA256: 9df9e67ec7fab2c2a153e4c02c38ae12de9f64274e491839169f11d52b91e15b
  • Pointer size: 132 Bytes
  • Size of remote file: 1.51 MB
2.png ADDED
3.png ADDED
README.md CHANGED
@@ -1,12 +1,2 @@
1
- ---
2
- title: Nexaagent
3
- emoji: πŸ’»
4
- colorFrom: pink
5
- colorTo: purple
6
- sdk: streamlit
7
- sdk_version: 1.27.2
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # autogen_langchain_private
2
+
 
 
 
 
 
 
 
 
 
 
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ requests
2
+ beautifulsoup4
3
+ streamlit
4
+ pyautogen~=0.1.0
5
+ docker
6
+ langchain
7
+ openai
8
+ tiktoken
9
+ chromadb
10
+ pypdf
streamlit_test_catch.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import autogen
4
+ import base64
5
+ from langchain.vectorstores import Chroma
6
+ from langchain.embeddings import OpenAIEmbeddings
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain.document_loaders import PyPDFLoader
9
+ from langchain.memory import ConversationBufferMemory
10
+ from langchain.llms import OpenAI
11
+ from langchain.chains import ConversationalRetrievalChain
12
+ import io
13
+ import sys
14
+ import tempfile
15
+ import openai
16
+ import multiprocessing
17
+ import autogen.agentchat.user_proxy_agent as upa
18
+
19
+ class OutputCapture:
20
+ def __init__(self):
21
+ self.contents = []
22
+
23
+ def write(self, data):
24
+ self.contents.append(data)
25
+
26
+ def flush(self):
27
+ pass
28
+
29
+ def get_output_as_string(self):
30
+ return ''.join(self.contents)
31
+
32
+ class ExtendedUserProxyAgent(upa.UserProxyAgent):
33
+ def __init__(self, *args, log_file="interaction_log.txt", **kwargs):
34
+ super().__init__(*args, **kwargs)
35
+ self.log_file = log_file
36
+
37
+ def log_interaction(self, message):
38
+ with open(self.log_file, "a") as f:
39
+ f.write(message + "\n")
40
+
41
+ def get_human_input(self, *args, **kwargs):
42
+ human_input = super().get_human_input(*args, **kwargs)
43
+ self.log_interaction(f"Human input: {human_input}")
44
+ return human_input
45
+
46
+ # Example usage:
47
+ config_list = [
48
+ {
49
+ "model": "gpt-4",
50
+ "api_key": "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",
51
+ }
52
+ ]
53
+
54
+ gpt4_api_key = config_list[0]["api_key"]
55
+ os.environ['OPENAI_API_KEY'] = gpt4_api_key
56
+ openai.api_key = os.environ["OPENAI_API_KEY"]
57
+
58
+ def build_vector_store(pdf_path, chunk_size=1000):
59
+ loaders = [PyPDFLoader(pdf_path)]
60
+ docs = []
61
+ for l in loaders:
62
+ docs.extend(l.load())
63
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size)
64
+ docs = text_splitter.split_documents(docs)
65
+ vectorstore = Chroma(
66
+ collection_name="full_documents",
67
+ embedding_function=OpenAIEmbeddings()
68
+ )
69
+ vectorstore.add_documents(docs)
70
+ return vectorstore
71
+
72
+ def setup_qa_chain(vectorstore):
73
+ qa = ConversationalRetrievalChain.from_llm(
74
+ OpenAI(temperature=0),
75
+ vectorstore.as_retriever(),
76
+ memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True)
77
+ )
78
+ return qa
79
+
80
+ def get_image_as_base64_string(path):
81
+ with open(path, "rb") as image_file:
82
+ return base64.b64encode(image_file.read()).decode()
83
+
84
+ def answer_uniswap_question(question, qa_chain):
85
+ response = qa_chain({"question": question})
86
+ return response["answer"]
87
+
88
+ def setup_agents(config_list, answer_function):
89
+ llm_config = {
90
+ "request_timeout": 600,
91
+ "seed": 42,
92
+ "config_list": config_list,
93
+ "temperature": 0,
94
+ "functions": [
95
+ {
96
+ "name": "answer_uniswap_question",
97
+ "description": "Answer any Uniswap related questions",
98
+ "parameters": {
99
+ "type": "object",
100
+ "properties": {
101
+ "question": {
102
+ "type": "string",
103
+ "description": "The question to ask in relation to Uniswap protocol",
104
+ }
105
+ },
106
+ "required": ["question"],
107
+ },
108
+ }
109
+ ],
110
+ }
111
+ assistant = autogen.AssistantAgent(name="assistant", llm_config=llm_config)
112
+ user_proxy = ExtendedUserProxyAgent(
113
+ name="user_proxy",
114
+ human_input_mode="NEVER",
115
+ max_consecutive_auto_reply=10,
116
+ code_execution_config={"work_dir": "."},
117
+ llm_config=llm_config,
118
+ system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
119
+ Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""",
120
+ function_map={"answer_uniswap_question": answer_function}
121
+ )
122
+ return assistant, user_proxy
123
+
124
+ def initiate_task(user_proxy, assistant, user_question):
125
+ user_proxy.initiate_chat(
126
+ assistant,
127
+ message= user_question
128
+ )
129
+
130
+ def initiate_task_process(queue, tmp_path, user_question):
131
+ loaders = [PyPDFLoader(tmp_path)]
132
+ vectorstore = build_vector_store(tmp_path)
133
+ qa_chain = setup_qa_chain(vectorstore)
134
+ assistant, user_proxy = setup_agents(config_list, lambda q: answer_uniswap_question(q, qa_chain))
135
+
136
+ output_capture = OutputCapture()
137
+ sys.stdout = output_capture
138
+ initiate_task(user_proxy, assistant, user_question)
139
+ queue.put(output_capture.get_output_as_string())
140
+
141
+ def app():
142
+ st.title("NexaAgent 0.0.1")
143
+
144
+ # Sidebar introduction
145
+ st.sidebar.header("About NexaAgent 0.0.1")
146
+ st.sidebar.markdown("""
147
+ πŸš€ **Introducing NexaAgent 0.0.1!**
148
+ A highly efficient PDF tool for all your needs.
149
+
150
+ πŸ“„ Upload any PDF, no matter its size or the task type.
151
+
152
+ βœ… Guaranteed accuracy, significantly reducing any discrepancies.
153
+
154
+ πŸ”§ Empowered by:
155
+ - **AutoGen** πŸ› οΈ
156
+ - **LangChain** 🌐
157
+ - **chromadb** πŸ—„οΈ
158
+ """)
159
+ image_path = "1.png"
160
+ st.sidebar.image(image_path, caption="Your Caption Here", use_column_width=True)
161
+
162
+
163
+ # Create left and right columns
164
+ col1, col2 = st.columns(2)
165
+
166
+ with col1:
167
+ # Upload PDF file
168
+ uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
169
+
170
+ if uploaded_file:
171
+ with st.spinner("Processing PDF..."):
172
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
173
+ tmp.write(uploaded_file.getvalue())
174
+ tmp_path = tmp.name
175
+
176
+ # User input for question
177
+ user_question = st.text_area("Enter your task:", height=300)
178
+ if user_question:
179
+ with st.spinner("Fetching the answer..."):
180
+ # 使用进程ζ₯ζ‰§θ‘Œε―θƒ½εΌ•ε‘ι”™θ――ηš„δ»£η 
181
+ queue = multiprocessing.Queue()
182
+ process = multiprocessing.Process(target=initiate_task_process, args=(queue, tmp_path, user_question))
183
+ process.start()
184
+ process.join()
185
+
186
+ # δ»Žι˜Ÿεˆ—δΈ­θŽ·ε–η»“ζžœ
187
+ captured_output = queue.get()
188
+ col2.text_area("", value=captured_output, height=600)
189
+
190
+ if __name__ == "__main__":
191
+ st.set_page_config(layout="wide")
192
+ app()
193
+
194
+
195
+
196
+
197
+
198
+