Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index.llms.openai import OpenAI
|
2 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
3 |
+
from llama_index.core import Settings
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import streamlit as st
|
7 |
+
import requests
|
8 |
+
|
9 |
+
adminkey = "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"
|
10 |
+
os.environ["OPENAI_API_KEY"] = adminkey
|
11 |
+
|
12 |
+
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4)
|
13 |
+
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
|
14 |
+
|
15 |
+
from opensearchpy import OpenSearch, RequestsHttpConnection
|
16 |
+
|
17 |
+
auth = ('admin','klbvrR4AlGNMaQ')
|
18 |
+
|
19 |
+
host = '10.11.10.111'
|
20 |
+
port = 32000
|
21 |
+
|
22 |
+
client = OpenSearch(
|
23 |
+
hosts = [{'host': host, 'port': port}],
|
24 |
+
http_auth = auth,
|
25 |
+
use_ssl = True,
|
26 |
+
verify_certs = False
|
27 |
+
)
|
28 |
+
|
29 |
+
def generate_opensearch_query(user_input):
|
30 |
+
prompt = f"""
|
31 |
+
You are an assistant trained to translate natural language requests into OpenSearch queries. Based on the user's request, generate an OpenSearch JSON query.
|
32 |
+
|
33 |
+
Examples:
|
34 |
+
User Input: "Get all documents where the status is active."
|
35 |
+
Response:
|
36 |
+
{{
|
37 |
+
"query": {{
|
38 |
+
"match": {{
|
39 |
+
"status": "active"
|
40 |
+
}}
|
41 |
+
}}
|
42 |
+
}}
|
43 |
+
|
44 |
+
User Input: "Find records with priority high created in the last 7 days."
|
45 |
+
Response:
|
46 |
+
{{
|
47 |
+
"query": {{
|
48 |
+
"bool": {{
|
49 |
+
"must": [
|
50 |
+
{{ "match": {{ "priority": "high" }} }},
|
51 |
+
{{ "range": {{ "created_at": {{ "gte": "now-7d/d", "lte": "now" }} }} }}
|
52 |
+
]
|
53 |
+
}}
|
54 |
+
}}
|
55 |
+
}}
|
56 |
+
|
57 |
+
User Input: "Show documents where age is over 30 and sort by created date."
|
58 |
+
Response:
|
59 |
+
{{
|
60 |
+
"query": {{
|
61 |
+
"range": {{
|
62 |
+
"age": {{ "gt": 30 }}
|
63 |
+
}}
|
64 |
+
}},
|
65 |
+
"sort": [
|
66 |
+
{{ "created_date": {{ "order": "asc" }} }}
|
67 |
+
]
|
68 |
+
}}
|
69 |
+
|
70 |
+
User Input: "{user_input}"
|
71 |
+
Response:
|
72 |
+
"""
|
73 |
+
|
74 |
+
llm_response = Settings.llm.complete(prompt)
|
75 |
+
return llm_response
|
76 |
+
|
77 |
+
def implement_query(generated_query):
|
78 |
+
query = json.loads(generated_query.text)
|
79 |
+
response = client.search(body=query)
|
80 |
+
return response
|
81 |
+
|
82 |
+
|
83 |
+
st.title("OpenSearch Query Generator")
|
84 |
+
st.subheader("Enter your natural language query:")
|
85 |
+
|
86 |
+
user_input = st.text_area("Enter a Prompt:", height=150)
|
87 |
+
|
88 |
+
if st.button("Generate OpenSearch Query"):
|
89 |
+
if user_input.strip():
|
90 |
+
generated_query = generate_opensearch_query(user_input)
|
91 |
+
|
92 |
+
st.subheader("Generated OpenSearch Query:")
|
93 |
+
st.json(json.loads(generated_query.text))
|
94 |
+
|
95 |
+
try:
|
96 |
+
response = implement_query(generated_query)
|
97 |
+
st.subheader("OpenSearch Response:")
|
98 |
+
st.json(response)
|
99 |
+
except Exception as e:
|
100 |
+
st.error(f"Error executing OpenSearch query: {e}")
|
101 |
+
else:
|
102 |
+
st.warning("Please enter a valid query.")
|