Spaces:
Sleeping
Sleeping
Ankush Rana
commited on
Commit
·
76d9486
1
Parent(s):
259e7ee
add vector store but not working
Browse files- app.py +2 -3
- data/vs/index.faiss +0 -0
- data/vs/index.pkl +3 -0
- requirements.txt +5 -1
- tools.py +24 -7
app.py
CHANGED
@@ -17,8 +17,7 @@ SYSTEM_PROMPT_TEMPLATE = """You are an AI assistant designed to assist users wit
|
|
17 |
Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience.
|
18 |
Always respond in the same language as the user’s query to preserve their preferred language.
|
19 |
|
20 |
-
|
21 |
-
"Welcome to Nou Vall de Núria! How can I assist you today?"""
|
22 |
|
23 |
|
24 |
# print(json.dumps(oitools, indent=2))
|
@@ -115,7 +114,7 @@ def llm_in_loop(history, system_prompt, recursive):
|
|
115 |
# metadata= {"title": f"🛠️ Using tool '{name}', arguments: {json.dumps(json_arguments, ensure_ascii=False)}"},
|
116 |
# options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
|
117 |
# )
|
118 |
-
history.append(ChatMessage(role="assistant", content=result, metadata={"title": json.dumps([{"id": "call_id", "function": {"arguments": json.dumps(arguments), "name": name}, "type": "function"}])}))
|
119 |
yield history[recursive:]
|
120 |
yield from llm_in_loop(history, system_prompt, recursive - 1)
|
121 |
|
|
|
17 |
Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience.
|
18 |
Always respond in the same language as the user’s query to preserve their preferred language.
|
19 |
|
20 |
+
If the user asks for any information about it's history, a region, its cities, activities, tourism, or surroundings, please use the function `get_documents`."""
|
|
|
21 |
|
22 |
|
23 |
# print(json.dumps(oitools, indent=2))
|
|
|
114 |
# metadata= {"title": f"🛠️ Using tool '{name}', arguments: {json.dumps(json_arguments, ensure_ascii=False)}"},
|
115 |
# options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
|
116 |
# )
|
117 |
+
history.append(ChatMessage(role="assistant", content=result, metadata={"title": json.dumps([{"id": "call_id", "function": {"arguments": json.dumps(arguments, ensure_ascii=False), "name": name}, "type": "function"}], ensure_ascii=False)}))
|
118 |
yield history[recursive:]
|
119 |
yield from llm_in_loop(history, system_prompt, recursive - 1)
|
120 |
|
data/vs/index.faiss
ADDED
Binary file (156 kB). View file
|
|
data/vs/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8f27a1cde9fae66a2551e256aa50771bcd4eb6db518f080751c43b1066e6cbb1
|
3 |
+
size 83028
|
requirements.txt
CHANGED
@@ -1,3 +1,7 @@
|
|
1 |
gradio==5.15.0
|
2 |
openai==1.61.0
|
3 |
-
python-dotenv==1.0.1
|
|
|
|
|
|
|
|
|
|
1 |
gradio==5.15.0
|
2 |
openai==1.61.0
|
3 |
+
python-dotenv==1.0.1
|
4 |
+
langchain-community==0.2.1
|
5 |
+
langchain-core==0.2.1
|
6 |
+
faiss-cpu==1.10.0
|
7 |
+
sentence-transformers
|
tools.py
CHANGED
@@ -5,14 +5,29 @@ import os
|
|
5 |
# from langchain.tools import tool
|
6 |
import json
|
7 |
from pydantic import BaseModel, Field
|
8 |
-
from
|
9 |
-
from typing import Dict, Optional, Union
|
10 |
import random
|
11 |
import copy
|
12 |
from types import UnionType
|
13 |
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def read_json(data_path: str) -> tuple[list, dict]:
|
17 |
try:
|
18 |
with open(data_path, 'r', encoding="utf-8") as f:
|
@@ -93,6 +108,7 @@ class ToolBase(BaseModel, ABC):
|
|
93 |
|
94 |
tools: Dict[str, ToolBase] = {}
|
95 |
oitools = []
|
|
|
96 |
|
97 |
def tool_register(cls: BaseModel):
|
98 |
oaitool = cls.to_openai_tool()
|
@@ -113,11 +129,11 @@ For an unforgettable stay, guests can choose from **special packages**, includin
|
|
113 |
Whether for relaxation or adventure, **Nou Vall de Núria** promises a unique and memorable experience."""
|
114 |
|
115 |
@tool_register
|
116 |
-
class
|
117 |
"""
|
118 |
-
Retrieves information about
|
119 |
"""
|
120 |
-
query: str = Field(description="
|
121 |
|
122 |
@classmethod
|
123 |
def invoke(cls, input: Dict) -> str:
|
@@ -125,7 +141,8 @@ class retrieve_hotel_info(ToolBase):
|
|
125 |
if not query:
|
126 |
return "Missing required argument: query."
|
127 |
|
128 |
-
return "We are currently working on it. You can't use this tool right now—please try again later. Thank you for your patience!"
|
|
|
129 |
|
130 |
# @tool_register
|
131 |
class hotel_facilities(ToolBase):
|
|
|
5 |
# from langchain.tools import tool
|
6 |
import json
|
7 |
from pydantic import BaseModel, Field
|
8 |
+
from typing import Dict, Union
|
|
|
9 |
import random
|
10 |
import copy
|
11 |
from types import UnionType
|
12 |
|
13 |
+
from langchain.vectorstores import FAISS
|
14 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
15 |
|
16 |
+
class VectorStore:
|
17 |
+
def __init__(self, embeddings_model, vectorstore):
|
18 |
+
embeddings = HuggingFaceEmbeddings(model_name=embeddings_model, model_kwargs={'device': 'cpu'})
|
19 |
+
self.vectore_store = FAISS.load_local(vectorstore, embeddings, allow_dangerous_deserialization=True)
|
20 |
+
|
21 |
+
def get_context(self, instruction, number_of_contexts=2):
|
22 |
+
documentos = self.vectore_store.similarity_search_with_score(instruction, k=number_of_contexts)
|
23 |
+
return self._beautiful_context(documentos)
|
24 |
+
|
25 |
+
def _beautiful_context(self, docs):
|
26 |
+
context = ""
|
27 |
+
for doc in docs:
|
28 |
+
context += doc[0].page_content + "\n"
|
29 |
+
return context
|
30 |
+
|
31 |
def read_json(data_path: str) -> tuple[list, dict]:
|
32 |
try:
|
33 |
with open(data_path, 'r', encoding="utf-8") as f:
|
|
|
108 |
|
109 |
tools: Dict[str, ToolBase] = {}
|
110 |
oitools = []
|
111 |
+
# vector_store = VectorStore(embeddings_model="BAAI/bge-m3", vectorstore="data/vs")
|
112 |
|
113 |
def tool_register(cls: BaseModel):
|
114 |
oaitool = cls.to_openai_tool()
|
|
|
129 |
Whether for relaxation or adventure, **Nou Vall de Núria** promises a unique and memorable experience."""
|
130 |
|
131 |
@tool_register
|
132 |
+
class get_documents(ToolBase):
|
133 |
"""
|
134 |
+
Retrieves general information about a region, its cities, activities, tourism, or surrounding areas based on query.
|
135 |
"""
|
136 |
+
query: str = Field(description="An enhanced user query optimized for retrieving information")
|
137 |
|
138 |
@classmethod
|
139 |
def invoke(cls, input: Dict) -> str:
|
|
|
141 |
if not query:
|
142 |
return "Missing required argument: query."
|
143 |
|
144 |
+
# return "We are currently working on it. You can't use this tool right now—please try again later. Thank you for your patience!"
|
145 |
+
# return vector_store.get_context(query)
|
146 |
|
147 |
# @tool_register
|
148 |
class hotel_facilities(ToolBase):
|