-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfllm.py
81 lines (57 loc) · 2.49 KB
/
cfllm.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
77
78
79
80
81
import requests
from dotenv import load_dotenv
import os
#Please don't forget this line ever again in the future-
load_dotenv()
#This will error out if no env variables are set!!
CLOUDFLARE_AI_GATEWAY_SLUG = os.getenv("CLOUDFLARE_AI_GATEWAY_SLUG")
CLOUDFLARE_USER_ID = os.getenv("CLOUDFLARE_USER_ID")
CLOUDFLARE_AI_API_KEY = os.getenv("CLOUDFLARE_AI_API_KEY")
API_BASE_URL = "https://gateway.ai.cloudflare.com/v1/"+CLOUDFLARE_USER_ID+"/"+CLOUDFLARE_AI_GATEWAY_SLUG+"/workers-ai/"
headers = {"Authorization": "Bearer "+CLOUDFLARE_AI_API_KEY}
def run(model, inputs):
input = { "stream": True, "messages": inputs }
response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
return response
selectedModel = "@cf/meta/llama-3-8b-instruct"
#streamed response
#TODO make this not error out for single token responses
def llmrequest(syst, prompt):
inputs = [
{ "role": "system", "content": syst }
]
inputs.append({ "role": "user", "content": prompt })
plaintext = ""
fullresp = ""
output = run(selectedModel, inputs)
for line in output:
if line: # Is it actually a new line?
# Handle each line of the response (text generation)
plaintext += line.decode("utf-8")
while plaintext.find('"response":') != -1:
whereis = plaintext.find('"response":')
plaintext = plaintext[whereis+12:]
whereto = plaintext.find('"')
fullresp += plaintext[:whereto]
if fullresp[-1] == fullresp[-2]:
fullresp = fullresp[:-1]
while fullresp[0] == " ":
fullresp = fullresp[1:]
return(True, str(fullresp))
#run but not streamed
def nsrun(model, inputs):
input = { "messages": inputs }
response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
return response
#TODO for the whole thing: make error handling just in case cloudflare AI freaks out
#It has happened and AI gateway even seems to cache errors which is very weird...
#non-streamed response (useful for very short expected responses (like single token level short) (the stream one seems to error out for that))
def nsllmreq(syst, prompt):
inputs = [
{ "role": "system", "content": syst }
]
inputs.append({ "role": "user", "content": prompt })
output = nsrun(selectedModel, inputs).json()
return output['result']['response']
if __name__ == "__main__":
print(nsllmreq("You are a helpful assistant. Respond with short messages.", "Why do humans live on?"))