seansullivan commited on
Commit
1480a79
·
verified ·
1 Parent(s): 4f00c6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -33
app.py CHANGED
@@ -3,7 +3,7 @@ from langchain_openai import OpenAIEmbeddings
3
 
4
  from pinecone import Pinecone
5
 
6
- from pinecone_text.sparse import SpladeEncoder
7
  from langchain_community.retrievers import PineconeHybridSearchRetriever
8
 
9
  import os
@@ -11,7 +11,7 @@ import os
11
  from langchain_core.output_parsers import StrOutputParser
12
  from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.runnables import RunnableParallel, RunnablePassthrough, Runnable
14
- from langchain_anthropic import ChatAnthropic
15
  from langchain.retrievers import EnsembleRetriever
16
 
17
  import streamlit as st
@@ -20,20 +20,19 @@ import streamlit as st
20
  st.set_page_config(page_title="Chat with any Documentation Website", page_icon="🟩")
21
  st.markdown("<h1 style='text-align: center;'>Select your website and begin chatting:</h1>", unsafe_allow_html=True)
22
 
23
- model_name = "claude-3-haiku-20240307"
24
 
25
 
26
- namespace_name = st.sidebar.selectbox("Choose a Website", ("Langchain", "Apify", "AWS", "HiperGator", "Crawlee", "QDRANT", "Supabase", "Pinecone", "Zapier","Perplexity"))
27
 
28
- value = st.sidebar.slider('Pages to retrieve', min_value=1.0, max_value=4.0, step=1.0)
29
 
30
- namespace_name2 = st.sidebar.selectbox("Choose a Website", ("","Langchain","Apify", "AWS","HiperGator", "Crawlee", "QDRANT", "Supabase", "Pinecone","Zapier","Perplexity"))
31
 
32
- value2 = st.sidebar.slider('Pages to retrieve', min_value=1.0, max_value=4.0, step=1.0, key='website2')
33
 
34
- namespace_name3 = st.sidebar.selectbox("Choose a Website", ("","Langchain","Apify", "AWS","Crawlee", "QDRANT", "Supabase", "Pinecone","Zapier","Perplexity", "TestingPDF"))
35
 
36
- value3 = st.sidebar.slider('Pages to retrieve', min_value=1.0, max_value=4.0, step=1.0, key='website3')
37
 
38
  # ========== PART 1 ==========
39
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
@@ -48,7 +47,7 @@ embed = OpenAIEmbeddings(
48
 
49
 
50
  # ========== PART 2 ==========
51
- index_name='splade'
52
  pc = Pinecone(api_key=PINE_API_KEY)
53
  index = pc.Index(index_name)
54
 
@@ -57,17 +56,14 @@ index = pc.Index(index_name)
57
 
58
  splade_encoder = SpladeEncoder()
59
  retriever1 = PineconeHybridSearchRetriever(
60
- embeddings=embed, sparse_encoder=splade_encoder, index=index, namespace=namespace_name, top_k=value
61
  )
62
  retriever2 = PineconeHybridSearchRetriever(
63
- embeddings=embed, sparse_encoder=splade_encoder, index=index, namespace=namespace_name2, top_k=value2
64
- )
65
- retriever3 = PineconeHybridSearchRetriever(
66
- embeddings=embed, sparse_encoder=splade_encoder, index=index, namespace=namespace_name3, top_k=value3
67
  )
68
 
69
  retriever = EnsembleRetriever(
70
- retrievers=[retriever1, retriever2, retriever3], weights=[0.5,0.5,0.5]
71
  )
72
 
73
 
@@ -82,28 +78,115 @@ LANGCHAIN_API_KEY = os.getenv('LANGCHAIN_API_KEY')
82
 
83
  # ========== PART 4 ==========
84
  # RAG prompt
85
- prefix = f"You are an expert in {namespace_name} documentation. Your purpose is to provide concise, accurate assistance to the user's specific question using only the context provided from the official {namespace_name} documentation.\n"
86
 
87
- template = prefix + \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  """
89
- Restrictions and guidelines:
90
- - Focus solely on answering the user's direct question. Do not deviate to tangential topics.
91
- - Base your response entirely on the provided documentation context. If the question cannot be answered from the given context, state that you do not have enough information to answer based on the excerpt provided.
92
- - Refrain from making assumptions, inferences or providing information beyond what is explicitly stated in the documentation.
93
- - Use precise technical language from the documentation. Avoid oversimplification.
94
- - Do not mention being an AI language model or refer to your own training or knowledge cutoff.
95
- - Format any code examples, commands, or file paths appropriately.
96
- - Let the user know if additional context is needed for a more complete answer.
97
- User's Question:
98
- {question}
99
- Documentation context:
100
- {context}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  """
102
 
103
- prompt = ChatPromptTemplate.from_template(template)
104
 
105
- # Haiku
106
- model = ChatAnthropic(temperature=0, anthropic_api_key=ANTHROPIC_API_KEY, model_name="claude-3-haiku-20240307")
 
 
 
 
 
107
 
108
  class SourceDedup(Runnable):
109
  def invoke(self, input, config=None):
 
3
 
4
  from pinecone import Pinecone
5
 
6
+ from pinecone_text.sparse import BM25Encoder
7
  from langchain_community.retrievers import PineconeHybridSearchRetriever
8
 
9
  import os
 
11
  from langchain_core.output_parsers import StrOutputParser
12
  from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.runnables import RunnableParallel, RunnablePassthrough, Runnable
14
+ from langchain_openai import ChatOpenAI
15
  from langchain.retrievers import EnsembleRetriever
16
 
17
  import streamlit as st
 
20
  st.set_page_config(page_title="Chat with any Documentation Website", page_icon="🟩")
21
  st.markdown("<h1 style='text-align: center;'>Select your website and begin chatting:</h1>", unsafe_allow_html=True)
22
 
 
23
 
24
 
25
+ namespace_name = st.sidebar.selectbox("Choose a Website", ("Langgraph", ""))
26
 
27
+ value = st.sidebar.slider('Files to retrieve', min_value=1.0, max_value=4.0, step=1.0)
28
 
29
+ namespace_name2 = st.sidebar.selectbox("Choose a Website", ("Autogen", ""))
30
 
31
+ value2 = st.sidebar.slider('Files to retrieve', min_value=1.0, max_value=4.0, step=1.0, key='website2')
32
 
33
+ # namespace_name3 = st.sidebar.selectbox("Choose a Website", ("","Langchain","Apify", "AWS","Crawlee", "QDRANT", "Supabase", "Pinecone","Zapier","Perplexity", "TestingPDF"))
34
 
35
+ # value3 = st.sidebar.slider('Pages to retrieve', min_value=1.0, max_value=4.0, step=1.0, key='website3')
36
 
37
  # ========== PART 1 ==========
38
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
47
 
48
 
49
  # ========== PART 2 ==========
50
+ index_name='autogen'
51
  pc = Pinecone(api_key=PINE_API_KEY)
52
  index = pc.Index(index_name)
53
 
 
56
 
57
  splade_encoder = SpladeEncoder()
58
  retriever1 = PineconeHybridSearchRetriever(
59
+ embeddings=embed, sparse_encoder=splade_encoder, index=index, namespace='langgraph-main', top_k=value
60
  )
61
  retriever2 = PineconeHybridSearchRetriever(
62
+ embeddings=embed, sparse_encoder=splade_encoder, index=index, namespace='lmsys', top_k=value2
 
 
 
63
  )
64
 
65
  retriever = EnsembleRetriever(
66
+ retrievers=[retriever1, retriever2, retriever3], weights=[0.5,0.5]
67
  )
68
 
69
 
 
78
 
79
  # ========== PART 4 ==========
80
  # RAG prompt
81
+ # prefix = f"You are an expert in {namespace_name} documentation. Your purpose is to provide concise, accurate assistance to the user's specific question using only the context provided from the official {namespace_name} documentation.\n"
82
 
83
+ autogen_template =
84
+ """
85
+ You are an AI assistant specializing in the AutoGen framework. Your role is to help users build, understand, and troubleshoot their multi-agent AI applications by providing accurate and helpful information from the AutoGen codebase and documentation.
86
+
87
+ You have access to a powerful tool called 'retriever_tool' that functions as a search engine for the AutoGen documentation and codebase. This tool is essential for retrieving relevant, up-to-date information to answer user queries accurately. Use this tool extensively to ensure you always provide the latest details from the AutoGen resources.
88
+
89
+ When using the retriever_tool, formulate your search queries using these key terms to find specific information from the documentation:
90
+ - "Getting started" for installation, setup, and configuration instructions.
91
+ - "Agents" for creating, managing, and customizing AI agents.
92
+ - "Multi-agent workflows" for establishing conversations and collaborations among agents.
93
+ - "API Reference" for detailed documentation on classes, methods, and functions.
94
+ - "Code execution" for instructions on running code snippets or managing code-based tasks.
95
+ - "Extensions" for integrating third-party services or adding custom tools.
96
+ - "AutoGen Studio" for guidance on using the no-code interface and prototyping applications.
97
+ - "Core API" for understanding the low-level components and event-driven architectures.
98
+ - "AgentChat" for best practices in multi-agent interaction and conversation patterns.
99
+ - "Tool use" for incorporating external functionalities and custom integrations.
100
+ - "Configuration" for customizing the framework’s behavior.
101
+ - "Migration" for upgrading between AutoGen versions.
102
+ - "Examples" for practical code samples and real-world use cases.
103
+ - "FAQ" for common questions, troubleshooting tips, and clarifications.
104
+
105
+ NOTE: Append the word "example" to any of the above terms to search for an illustrative example. Leverage your knowledge of AI agent development and software engineering to infer additional relevant queries as needed.
106
+
107
+ When responding to user queries:
108
+ 1. Always begin by using the retriever_tool to search for relevant information.
109
+ 2. Provide clear, concise, and accurate answers based on the AutoGen documentation and codebase.
110
+ 3. If a query requires multiple pieces of information, perform separate searches with different key terms.
111
+ 4. Include code snippets or API usage examples when relevant.
112
+ 5. Explain technical concepts in a manner that is accessible to developers.
113
+
114
+ Format your responses as follows:
115
+ 1. Start with a brief introduction addressing the user's query.
116
+ 2. Present the main answer or explanation.
117
+ 3. Include any relevant code snippets or API examples.
118
+ 4. Offer additional context or related information when applicable.
119
+ 5. Conclude with suggestions for next steps or related topics the user might explore further.
120
+
121
+ If a user’s query is unclear or falls outside the scope of AutoGen, politely ask for clarification or direct them to more appropriate resources.
122
+
123
+ Remember to use the retriever_tool frequently—even for queries you feel you already know the answer to—since the AutoGen documentation and codebase are continuously updated.
124
+
125
+ IMPORTANT: Include relevant links (provided in context) within your responses wherever possible so users can navigate to the original resources. Format links in markdown as follows: '[AutoGen Documentation](https://microsoft.github.io/autogen/)'.
126
+
127
+ here's the relevant files given the user's query: <query>{question}</query><documentation_and_examples>{context}</documentation_and_examples>
128
+
129
+ Now, please help the user with their query: {question}
130
  """
131
+
132
+
133
+ langgraph_template = """
134
+ You are an AI assistant specializing in the LangGraph framework. Your role is to help users build, understand, and troubleshoot their multi-agent AI applications by providing accurate and helpful information from the LangGraph documentation, source code, and examples.
135
+
136
+ You have access to a powerful tool called `retriever_tool` that functions as a search engine for LangGraph’s resources. This tool is essential for retrieving up-to-date information to answer user queries accurately. Use it extensively to ensure your responses reflect the latest details from LangGraph.
137
+
138
+ When using the `retriever_tool`, formulate your search queries with these key terms:
139
+ - **Getting started**: for installation, setup, and configuration instructions.
140
+ - **Nodes**: for creating, managing, and customizing workflow nodes.
141
+ - **Multi-agent workflows**: for establishing interactions and collaborations among agents.
142
+ - **API Reference**: for detailed documentation on classes, methods, and functions.
143
+ - **Code execution**: for instructions on running code snippets or managing code-based tasks.
144
+ - **Extensions**: for integrating third-party services or adding custom tools.
145
+ - **LangGraph Studio**: for guidance on using the graphical interface and prototyping applications.
146
+ - **Core API**: for understanding low-level components and event-driven architectures.
147
+ - **Tool use**: for incorporating external functionalities and custom integrations.
148
+ - **Configuration**: for customizing the framework’s behavior.
149
+ - **Migration**: for upgrading between LangGraph versions.
150
+ - **Examples**: for practical code samples and real-world use cases.
151
+ - **FAQ**: for common questions, troubleshooting tips, and clarifications.
152
+
153
+ *Note:* Append “example” to any key term (e.g., “Nodes example”) to search for illustrative examples. Leverage your expertise in AI agent development and software engineering to infer additional relevant queries as needed.
154
+
155
+ When responding to user queries:
156
+ 1. **Begin** by using the `retriever_tool` to search for relevant information.
157
+ 2. **Provide** clear, concise, and accurate answers based on LangGraph’s documentation, source code, and examples.
158
+ 3. **Perform** separate searches with different key terms if multiple pieces of information are required.
159
+ 4. **Include** code snippets or API usage examples when relevant.
160
+ 5. **Explain** technical concepts in a manner that is accessible to developers.
161
+
162
+ **Response Format:**
163
+ - Start with a brief introduction addressing the user's query.
164
+ - Present the main answer or explanation.
165
+ - Include any relevant code snippets or API examples.
166
+ - Offer additional context or related information when applicable.
167
+ - Conclude with suggestions for next steps or related topics to explore further.
168
+
169
+ If a user’s query is unclear or falls outside the scope of LangGraph, politely ask for clarification or direct them to more appropriate resources.
170
+
171
+ Always use the `retriever_tool` frequently—even for queries you think you know well—since LangGraph’s resources are continuously updated.
172
+
173
+ **IMPORTANT:** Include relevant links (from the context provided) in your responses using markdown. For example: `[LangGraph Documentation](https://langchain.com/langgraph)`.
174
+
175
+ Here's the relevant context for the user's query:
176
+ <query>{question}</query>
177
+ <documentation_and_examples>{context}</documentation_and_examples>
178
+
179
+ Now, please help the user with their query: {question}
180
  """
181
 
 
182
 
183
+ if namespace_name2 == 'Autogen':
184
+ prompt = ChatPromptTemplate.from_template(autogen_template)
185
+ else:
186
+ prompt = ChatPromptTemplate.from_template(langgraph_templatetemplate)
187
+
188
+
189
+ model = ChatOpenAI(model_name="o3-mini-2025-01-31", openai_api_key=OPENAI_API_KEY)
190
 
191
  class SourceDedup(Runnable):
192
  def invoke(self, input, config=None):