Skip to content

Commit 4188a3b

Browse files
Created API endpoint
1 parent e6bbd2f commit 4188a3b

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

.DS_Store

-2 KB
Binary file not shown.

answer.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, json, requests
1+
import os, json, requests, chromadb
22
from dotenv import load_dotenv
33

44
load_dotenv()
@@ -10,5 +10,42 @@ def answer_user_question(question: str):
1010
headers = {"Authorization": f"Bearer {CLOUDFLARE_API_KEY}"}
1111
model = "@cf/baai/bge-small-en-v1.5"
1212
url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/{model}"
13+
payload = json.dumps({"text": [question]})
14+
response = requests.request("POST", url, headers=headers, data=payload)
15+
question_result = response.json()
16+
client = chromadb.PersistentClient(path="./chroma")
17+
collection = client.get_or_create_collection("Interviews")
18+
result = collection.query(query_embeddings=question_result["result"]["data"], n_results=2)
19+
results = result["documents"]
20+
return get_response_from_llm(question, results)
1321

14-
payload = json.dumps({"prompt": question})
22+
def get_response_from_llm(question, results):
23+
headers = {"Authorization": f"Bearer {CLOUDFLARE_API_KEY}"}
24+
payload = create_payload(question, results[0][0])
25+
model = "@cf/meta/llama-3.1-8b-instruct"
26+
url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/{model}"
27+
response = requests.request("POST", url, headers=headers, data=payload)
28+
question_result = response.json()
29+
30+
return question_result["result"]["response"]
31+
32+
33+
def create_payload(sentence, context):
34+
# input_prompt = (f"You are being interviewed by a company. Provide a brief summary of the question based on your knowledge. If you don't know the answer, just say that you don't know."
35+
# f"Context: {context} \nQuestion: {sentence}"
36+
# )
37+
38+
return json.dumps({
39+
"messages": [
40+
{
41+
"role": "system",
42+
"content": "You are being interviewed by a company. Provide a answer suitable to the question based on your knowledge. If you don't know the answer, just say that you don't know."
43+
},
44+
{
45+
"role": "user",
46+
"content": f"Context: {context}\nQuestion: {sentence}"
47+
}
48+
]
49+
})
50+
51+
print(answer_user_question("What is the difference between bias and variance trade off ?"))

app.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from flask import Flask, request
2+
from answer import answer_user_question
3+
4+
app = Flask(__name__)
5+
6+
@app.route("/ask-question", methods=["POST", "GET"])
7+
def ask_question():
8+
json_content = request.json
9+
question = json_content.get('question')
10+
11+
answer = answer_user_question(question)
12+
return {"question": question, "answer": answer}
13+
14+
@app.route("/")
15+
def show_api_info():
16+
return {
17+
"name": "RAG ChatBot API",
18+
"endpoints": [
19+
{
20+
"path": "/ask-question",
21+
"methods": ["POST", "GET"],
22+
"description": "Ask a question and get an AI-generated answer"
23+
}
24+
]
25+
}
26+
if __name__ == "__main__":
27+
app.run()
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)