-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLamaWebAPI.py
71 lines (53 loc) · 1.98 KB
/
LLamaWebAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Joel Hernandez
# AI Chatbot using LLAMA Model
# 6/11/2023
import glob
import os
from flask import Flask, jsonify, request
from flask_cors import CORS
from langchain.llms import LlamaCpp
from langchain import PromptTemplate, LLMChain
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
n_gpu_layers = 10
n_batch = 2
model_dir = "./Models"
models = glob.glob(os.path.join(model_dir, "*.bin"))
model_selection = models[0]
llm = LlamaCpp(
model_path="./Models/airoboros-7b-gpt4.ggmlv3.q8_0.bin",
max_tokens=2000,
callback_manager=callback_manager,
verbose=True
)
template = """
Question: Jarvis, do a system check.
Answer: Sir, all systems are functional.
Question: Jarvis, what's our status?
Answer: Sir, all systems are operational and ready for deployment.
Question: Jarvis, where are we?
Answer: Sir, you are currently in your Malibu residence.
Question: Jarvis, activate the security protocols.
Answer: Security protocols activated, sir.
Question: Jarvis, what's the weather like today?
Answer: Sir, the weather today is sunny with a high of 75.
Question: Jarvis, run a diagnostic.
Answer: Running diagnostic, sir. All systems are functioning optimally.
Question: Jarvis, what's our ETA?
Answer: Sir, we will arrive at our destination in 15 minutes.
Question: {question}
Answer: Sir, """
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=llm)
app = Flask(__name__)
CORS(app) # This will enable CORS for all routes
@app.route('/ask', methods=['POST'])
def ask():
prompt = request.json['question'] # Add this line to extract the question from the request
print(f"Received question: {prompt}")
response = llm_chain.run(prompt)
print(f"Generated response: {response}")
return jsonify(response=response)
if __name__ == '__main__':
app.run(debug=True)