Macmill commited on
Commit
e4c7fbd
·
verified ·
1 Parent(s): 8b6b4c4

Upload 2 files

Browse files
Files changed (2) hide show
  1. gradio_test.py +100 -0
  2. requirements.txt +0 -0
gradio_test.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import chromadb
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+ from llama_index.core import VectorStoreIndex
6
+ from llama_index.core.agent import ReActAgent
7
+ from llama_index.core.tools import QueryEngineTool
8
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
9
+ from llama_index.llms.gemini import Gemini
10
+ from llama_index.core.workflow import Context
11
+ from llama_index.vector_stores.chroma import ChromaVectorStore
12
+
13
+ load_dotenv()
14
+
15
+ agent = None
16
+ conversation_context = None
17
+
18
+ async def initialize_agent():
19
+ """Initialize the agent once"""
20
+ global agent, conversation_context
21
+ if agent is not None:
22
+ return agent, conversation_context
23
+
24
+ llm = Gemini(
25
+ model="models/gemini-flash-latest",
26
+ api_key=os.getenv("GEMINI_API"),
27
+ temperature=0.3,
28
+ )
29
+
30
+ embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
31
+
32
+ db = chromadb.PersistentClient(path="./product_db")
33
+ chroma_collection = db.get_collection(name="product_catalog")
34
+ vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
35
+
36
+ index = VectorStoreIndex.from_vector_store(
37
+ vector_store=vector_store,
38
+ embed_model=embed_model
39
+ )
40
+
41
+ query_engine = index.as_query_engine(llm=llm)
42
+
43
+ query_tool = QueryEngineTool.from_defaults(
44
+ query_engine=query_engine,
45
+ name="ProductInfoTool",
46
+ description="A tool to retrieve information about camping products, including their stock availability.",
47
+ )
48
+
49
+ agent = ReActAgent(
50
+ llm=llm,
51
+ tools=[query_tool],
52
+ verbose=False,
53
+ system_prompt="""You are a friendly and knowledgeable camping gear expert.
54
+ Your goal is to find the perfect product for the user and tell them about it in a helpful, conversational way.
55
+ Use the `ProductInfoTool` to find the best match for the user's query.
56
+ In your final response to the user, you MUST include the following three pieces of information:
57
+ 1. The full product name.
58
+ 2. A brief, one-sentence reason why it's a good choice for them.
59
+ 3. The exact stock status (e.g., '15 available' or 'out of stock').
60
+ If the tool cannot find a suitable product, just say: 'I'm sorry, I couldn't find a product that matches your request.'
61
+ Remember conversation context and refer back to previous messages when appropriate."""
62
+ )
63
+
64
+ conversation_context = Context(agent)
65
+
66
+ return agent, conversation_context
67
+
68
+
69
+ async def chat_with_agent(message, history):
70
+ """Handle chat messages with the agent"""
71
+ global conversation_context, agent
72
+
73
+ try:
74
+ agent, ctx = await initialize_agent()
75
+ response = await agent.run(message, ctx=conversation_context)
76
+ return str(response.response)
77
+ except Exception as e:
78
+ if "index out of range" in str(e):
79
+ conversation_context = Context(agent)
80
+ response = await agent.run(message, ctx=conversation_context)
81
+ return str(response.response)
82
+ return f"Sorry, I encountered an error: {str(e)}"
83
+
84
+
85
+ def main():
86
+ """Launch the simple Gradio interface"""
87
+ demo = gr.ChatInterface(
88
+ fn=chat_with_agent,
89
+ title="🏕️ Tory - The Camping Gear Assistant",
90
+ description="Ask me about camping products and I'll help you find the perfect gear!",
91
+ examples=[
92
+ "I need a lightweight tent for 2 people",
93
+ "What sleeping bags do you have?",
94
+ "Show me available camping stoves"
95
+ ],
96
+ )
97
+ demo.launch()
98
+
99
+ if __name__ == "__main__":
100
+ main()
requirements.txt ADDED
Binary file (6.59 kB). View file