-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_req_example.py
More file actions
90 lines (74 loc) · 2.05 KB
/
llm_req_example.py
File metadata and controls
90 lines (74 loc) · 2.05 KB
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
82
83
84
85
86
87
88
89
90
import requests
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Get values from environment variables
api_key = os.getenv('API_KEY')
account_id = os.getenv('ACCOUNT_ID')
ai_gateway = os.getenv('AI_GATEWAY')
autorag_name = os.getenv('AUTORAG_NAME')
autorag_api_key = os.getenv('AUTORAG_API_KEY')
draw_model = os.getenv('DRAW_MODEL')
# regular model search
url = f"https://gateway.ai.cloudflare.com/v1/{account_id}/{ai_gateway}/workers-ai/@cf/meta/llama-3.1-8b-instruct"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"messages": [
{
"role": "system",
"content": "You are a helpful AI assistant. Provide clear and informative responses."
},
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}
#auto rag search
url2 = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/autorag/rags/{autorag_name}/ai-search"
headers2 = {
"Authorization": f"Bearer {autorag_api_key}",
"Content-Type": "application/json"
}
data2 = {
"query": "What is Dota 2?"
}
#draw image
url3 = f"https://gateway.ai.cloudflare.com/v1/{account_id}/{ai_gateway}/workers-ai/{draw_model}"
headers3 = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data3 = {
"prompt": "cyberpunk cat"
}
#draw image output
"""
ReadableStream {
locked: false,
[state]: 'readable',
[supportsBYOB]: true,
[length]: undefined
}
{
"type": "string",
"contentType": "image/png",
"format": "binary",
"description": "The generated image in PNG format"
}
"""
try:
print("Testing regular model search")
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
print(response.json())
print("Testing autorag search")
response2 = requests.post(url2, headers=headers2, json=data2)
response2.raise_for_status()
print(response2.json())
except requests.exceptions.RequestException as e:
print(f"Error: {e}")