Skip to content

Commit 85c1e22

Browse files
committed
Fix
1 parent 6313254 commit 85c1e22

File tree

5 files changed

+19
-178
lines changed

5 files changed

+19
-178
lines changed

scenarios/PostgresRAGLLM/app.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

scenarios/PostgresRAGLLM/chat.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from db import VectorDatabase
99

1010
# Configure logging
11-
logging.basicConfig(level=logging.DEBUG)
11+
logging.basicConfig(level=logging.INFO)
1212

1313
parser = argparse.ArgumentParser()
1414
parser.add_argument('--api-key', dest='api_key', type=str)
@@ -18,13 +18,11 @@
1818
parser.add_argument('--pgpassword', dest='pgpassword', type=str)
1919
parser.add_argument('--pgdatabase', dest='pgdatabase', type=str)
2020
parser.add_argument('--populate', dest='populate', action="store_true")
21-
parser.add_argument('--question', dest='question', type=str, help="Question to ask the chatbot")
2221
args = parser.parse_args()
2322

2423

2524
class ChatBot:
2625
def __init__(self):
27-
logging.debug("Initializing ChatBot")
2826
self.db = VectorDatabase(pguser=args.pguser, pghost=args.phhost, pgpassword=args.pgpassword, pgdatabase=args.pgdatabase)
2927
self.api = AzureOpenAI(
3028
azure_endpoint=args.endpoint,
@@ -39,21 +37,17 @@ def __init__(self):
3937
)
4038

4139
def load_file(self, text_file: str):
42-
logging.debug(f"Loading file: {text_file}")
40+
logging.info(f"Loading file: {text_file}")
4341
with open(text_file, encoding="UTF-8") as f:
4442
data = f.read()
4543
chunks = self.text_splitter.create_documents([data])
4644
for i, chunk in enumerate(chunks):
4745
text = chunk.page_content
4846
embedding = self.__create_embedding(text)
4947
self.db.save_embedding(i, text, embedding)
50-
51-
def __create_embedding(self, text: str):
52-
logging.debug(f"Creating embedding for text: {text[:30]}...")
53-
return self.api.embeddings.create(model="text-embedding-ada-002", input=text).data[0].embedding
48+
logging.info("Done loading data.")
5449

5550
def get_answer(self, question: str):
56-
logging.debug(f"Getting answer for question: {question}")
5751
question_embedding = self.__create_embedding(question)
5852
context = self.db.search_documents(question_embedding)
5953

@@ -80,26 +74,21 @@ def get_answer(self, question: str):
8074
)
8175
return response.choices[0].message.content
8276

77+
def __create_embedding(self, text: str):
78+
return self.api.embeddings.create(model="text-embedding-ada-002", input=text).data[0].embedding
79+
8380

8481
def main():
8582
chat_bot = ChatBot()
8683

8784
if args.populate:
88-
logging.debug("Loading embedding data into database...")
8985
chat_bot.load_file("knowledge.txt")
90-
logging.debug("Done loading data.")
91-
return
92-
93-
if args.question:
94-
logging.debug(f"Question provided: {args.question}")
95-
print(chat_bot.get_answer(args.question))
96-
return
97-
98-
while True:
99-
q = input("Ask a question (q to exit): ")
100-
if q == "q":
101-
break
102-
print(chat_bot.get_answer(q))
86+
else:
87+
while True:
88+
q = input("Ask a question (q to exit): ")
89+
if q == "q":
90+
break
91+
print(chat_bot.get_answer(q))
10392

10493

10594
if __name__ == "__main__":

scenarios/PostgresRAGLLM/postgres-rag-llm.md

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -136,101 +136,12 @@ pip install -r requirements.txt
136136
python chat.py --populate --api-key $API_KEY --endpoint $ENDPOINT --pguser $PGUSER --phhost $PGHOST --pgpassword $PGPASSWORD --pgdatabase $PGDATABASE
137137
```
138138

139-
## Set up Web Interface
139+
## Run Chat bot
140140

141-
Create a simple web interface for the chatbot using Flask.
142-
143-
1. **Install Flask**
144-
145-
```bash
146-
pip install Flask
147-
```
148-
149-
2. **Create `app.py`**
150-
151-
Create a file named `app.py` in the `scenarios/PostgresRagLlmDemo` directory with the following content:
152-
153-
```python
154-
from flask import Flask, request, render_template
155-
import subprocess
156-
import os
157-
158-
app = Flask(__name__)
159-
160-
@app.route('/', methods=['GET'])
161-
def home():
162-
return render_template('index.html', response='')
163-
164-
@app.route('/ask', methods=['POST'])
165-
def ask():
166-
question = request.form['question']
167-
result = subprocess.run([
168-
'python', 'chat.py',
169-
'--api-key', os.getenv('API_KEY'),
170-
'--endpoint', os.getenv('ENDPOINT'),
171-
'--pguser', os.getenv('PGUSER'),
172-
'--phhost', os.getenv('PGHOST'),
173-
'--pgpassword', os.getenv('PGPASSWORD'),
174-
'--pgdatabase', os.getenv('PGDATABASE'),
175-
'--question', question
176-
], capture_output=True, text=True)
177-
response = result.stdout
178-
return render_template('index.html', response=response)
179-
180-
if __name__ == '__main__':
181-
app.run(host='0.0.0.0', port=5000)
182-
```
183-
184-
3. **Create `index.html`**
185-
186-
Create a `templates` directory inside `scenarios/PostgresRagLlmDemo` and add an `index.html` file with the following content:
187-
188-
```html
189-
<!doctype html>
190-
<html lang="en">
191-
<head>
192-
<title>Chatbot Interface</title>
193-
</head>
194-
<body>
195-
<h1>Ask about Zytonium</h1>
196-
<form action="/ask" method="post">
197-
<input type="text" name="question" required>
198-
<button type="submit">Ask</button>
199-
</form>
200-
<pre>{{ response }}</pre>
201-
</body>
202-
</html>
203-
```
204-
205-
4. **Run the Web Server**
206-
207-
Ensure that all environment variables are exported and then run the Flask application:
208-
209-
```bash
210-
export API_KEY="$API_KEY"
211-
export ENDPOINT="$ENDPOINT"
212-
export PGUSER="$PGUSER"
213-
export PGHOST="$PGHOST"
214-
export PGPASSWORD="$PGPASSWORD"
215-
export PGDATABASE="$PGDATABASE"
216-
217-
python app.py
218-
```
219-
220-
The web interface will be accessible at `http://localhost:5000`. You can ask questions about Zytonium through the browser.
221-
222-
## Next Steps
223-
224-
- Explore more features of [Azure Cognitive Search](https://learn.microsoft.com/azure/search/search-what-is-azure-search).
225-
- Learn how to [use Azure OpenAI with your data](https://learn.microsoft.com/azure/cognitive-services/openai/use-your-data).
226-
<!-- ## Run Chat bot
227-
228-
This final step initializes the chatbot in your terminal. You can ask it questions about Zytonium and it will use the embeddings in the postgres database to augment your query with relevant context before sending it to the LLM model.
229-
230-
```bash
231-
echo "Ask the chatbot a question about Zytonium!"
232-
```
141+
To run the chatbot, paste this following command to the terminal: `cd ~/scenarios/PostgresRagLlmDemo && python chat.py --api-key $API_KEY --endpoint $ENDPOINT --pguser $PGUSER --phhost $PGHOST --pgpassword $PGPASSWORD --pgdatabase $PGDATABASE`
233142

234143
```bash
235-
python chat.py --api-key $API_KEY --endpoint $ENDPOINT --pguser $PGUSER --phhost $PGHOST --pgpassword $PGPASSWORD --pgdatabase $PGDATABASE
236-
``` -->
144+
echo "
145+
To run the chatbot, see the last step for more info.
146+
"
147+
```
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
azure-identity==1.17.1
22
openai==1.55.3
33
psycopg2==2.9.9
4-
langchain-text-splitters==0.2.2
5-
Flask==2.3.2
4+
langchain-text-splitters==0.2.2

scenarios/PostgresRAGLLM/templates/index.html

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)