Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,16 +22,16 @@ auth = (user,password)
|
|
22 |
|
23 |
client = openai.OpenAI(api_key=openai_api_key)
|
24 |
|
25 |
-
os_client = OpenSearch(
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
)
|
31 |
-
indices = os_client.cat.indices(format="json")
|
32 |
-
list_of_indeces = []
|
33 |
-
for index in indices:
|
34 |
-
|
35 |
|
36 |
def rag_app(user_input: str) -> str:
|
37 |
gr_client = Client("anasmkh/QdrantVectorStore_Llamaindex")
|
@@ -49,12 +49,12 @@ def query_generator(user_input:str) -> str:
|
|
49 |
completion = client.chat.completions.create(
|
50 |
model=response.fine_tuned_model,
|
51 |
messages=[
|
52 |
-
{"role": "system", "content":
|
53 |
|
54 |
Understand the user's request by breaking it down into components such as filters, aggregations, sort criteria, and specific fields.
|
55 |
Pay special attention to fields with unique names, such as Date (instead of timestamp) and Stream (instead of type), and ensure they are used correctly in the query.
|
56 |
Recognize that the user operates within two main opcos: Zambia and Eswatini, each containing ptm_counters, ptm_events, and multiple streams like ers-daily.
|
57 |
-
Generate a valid JSON query strictly based on the provided indices, ensuring it aligns with the user's prompt.
|
58 |
When generating the query:
|
59 |
Be precise and include only necessary fields and components relevant to the request.
|
60 |
Assume any unspecified context or detail needs clarification and provide a clear explanation of your assumptions if needed.
|
@@ -72,27 +72,27 @@ query_tool = FunctionTool.from_defaults(fn=query_generator)
|
|
72 |
llm = OpenAI(model="gpt-3.5-turbo", temperature=0)
|
73 |
agent = ReActAgent.from_tools([query_tool,rag_tool], llm=llm, verbose=True)
|
74 |
|
75 |
-
def implement_query(generated_query):
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
|
98 |
st.subheader('OpenSearch Assistant')
|
@@ -111,9 +111,9 @@ if st.button("Submit"):
|
|
111 |
st.write('Used Tool: ',source.tool_name)
|
112 |
if source.tool_name =='query_generator':
|
113 |
st.write(source.raw_output.content)
|
114 |
-
os_response = implement_query(source.raw_output.content)
|
115 |
-
st.subheader('OS Response')
|
116 |
-
st.write(os_response)
|
117 |
else:
|
118 |
st.write(source.raw_output[0][0][1])
|
119 |
except Exception as e:
|
|
|
22 |
|
23 |
client = openai.OpenAI(api_key=openai_api_key)
|
24 |
|
25 |
+
# os_client = OpenSearch(
|
26 |
+
# hosts = [{'host': host, 'port': port}],
|
27 |
+
# http_auth = auth,
|
28 |
+
# use_ssl = True,
|
29 |
+
# verify_certs = False
|
30 |
+
# )
|
31 |
+
# indices = os_client.cat.indices(format="json")
|
32 |
+
# list_of_indeces = []
|
33 |
+
# for index in indices:
|
34 |
+
# list_of_indeces.append(index['index'])
|
35 |
|
36 |
def rag_app(user_input: str) -> str:
|
37 |
gr_client = Client("anasmkh/QdrantVectorStore_Llamaindex")
|
|
|
49 |
completion = client.chat.completions.create(
|
50 |
model=response.fine_tuned_model,
|
51 |
messages=[
|
52 |
+
{"role": "system", "content": """You are a highly skilled assistant trained to translate natural language requests into accurate and efficient OpenSearch JSON queries. Follow a clear, step-by-step process to:
|
53 |
|
54 |
Understand the user's request by breaking it down into components such as filters, aggregations, sort criteria, and specific fields.
|
55 |
Pay special attention to fields with unique names, such as Date (instead of timestamp) and Stream (instead of type), and ensure they are used correctly in the query.
|
56 |
Recognize that the user operates within two main opcos: Zambia and Eswatini, each containing ptm_counters, ptm_events, and multiple streams like ers-daily.
|
57 |
+
Generate a valid JSON query strictly based on the provided indices, ensuring it aligns with the user's prompt.
|
58 |
When generating the query:
|
59 |
Be precise and include only necessary fields and components relevant to the request.
|
60 |
Assume any unspecified context or detail needs clarification and provide a clear explanation of your assumptions if needed.
|
|
|
72 |
llm = OpenAI(model="gpt-3.5-turbo", temperature=0)
|
73 |
agent = ReActAgent.from_tools([query_tool,rag_tool], llm=llm, verbose=True)
|
74 |
|
75 |
+
# def implement_query(generated_query):
|
76 |
+
# try:
|
77 |
+
# st.write("Raw Query:", generated_query)
|
78 |
|
79 |
+
# if isinstance(generated_query, str):
|
80 |
+
# generated_query = generated_query.replace("'", '"')
|
81 |
+
# query = json.loads(generated_query)
|
82 |
+
# else:
|
83 |
+
# query = generated_query
|
84 |
|
85 |
+
# st.write("Validated Query:", query)
|
86 |
|
87 |
|
88 |
+
# response = os_client.search(body=query)
|
89 |
+
# return response
|
90 |
+
# except json.JSONDecodeError as e:
|
91 |
+
# st.error("Error: The generated query is not valid JSON.")
|
92 |
+
# st.write(f"JSONDecodeError Details: {e}")
|
93 |
+
# except Exception as e:
|
94 |
+
# st.error(f"Error executing OpenSearch query: {e}")
|
95 |
+
# st.write(f"Exception Details: {e}")
|
96 |
|
97 |
|
98 |
st.subheader('OpenSearch Assistant')
|
|
|
111 |
st.write('Used Tool: ',source.tool_name)
|
112 |
if source.tool_name =='query_generator':
|
113 |
st.write(source.raw_output.content)
|
114 |
+
# os_response = implement_query(source.raw_output.content)
|
115 |
+
# st.subheader('OS Response')
|
116 |
+
# st.write(os_response)
|
117 |
else:
|
118 |
st.write(source.raw_output[0][0][1])
|
119 |
except Exception as e:
|