-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_llama.py
76 lines (63 loc) · 2.6 KB
/
app_llama.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
72
73
74
75
76
from flask import Flask, Response, jsonify, request
import os
import json
import requests
from utility import *
app = Flask(__name__)
system_prompt = HIGH_LEVEL_SUMMARY_SYSTEM_PROMPT_WITHOUT_JSON_RESPONSE
# Llama API configuration
URI = os.environ.get('LLAMA_URI')
API_KEY = os.environ.get('LLAMA_KEY')
@app.route('/', methods=['GET'])
def home():
endpoints = sorted(r.rule for r in app.url_map.iter_rules())
return jsonify(endpoints=endpoints)
@app.route("/summary", methods=["POST"])
def summary():
if not API_KEY:
return jsonify({
"error": "Server configuration error.",
"message": "Llama API key is not configured in the server."
}), 500
data = request.json
if not isinstance(data, dict) or "query" not in data or "results" not in data:
return jsonify(error="Invalid request format. Must contain 'query' and 'results' keys."), 400
prompt = get_token_limited_prompt(data, system_prompt)
def generate():
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
"temperature": TEMPERATURE,
"max_tokens": MAX_TOKENS,
"stop": ["<|endoftext|>", "End of Response"],
"presence_penalty": 0.5,
"stream": True
}
with requests.post(URI, headers=headers, json=data, stream=True) as response:
if response.status_code == 200:
for line in response.iter_lines():
if line:
try:
json_str = line.decode('utf-8')
if json_str.startswith('data: '):
json_str = json_str[len('data: '):]
chunk = json.loads(json_str)
choices = chunk.get('choices', [])
if choices:
delta = choices[0].get('delta', {})
content = delta.get('content', '')
if content:
yield content
except (json.JSONDecodeError, IndexError, KeyError):
continue
else:
yield f"Error: Request failed with status code {response.status_code}"
return Response(generate(), mimetype='text/event-stream')
if __name__ == '__main__':
app.run(debug=True, port=5000)