-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_2.py
More file actions
41 lines (29 loc) · 1.17 KB
/
Copy pathPractical_2.py
File metadata and controls
41 lines (29 loc) · 1.17 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
import os
import json
from dotenv import load_dotenv
from cerebras.cloud.sdk import Cerebras
# TODO 1: Load .env file and assign the API key to Cerebras using OS library
client = Cerebras(
api_key=
)
def ask_cerebras(prompt: str, model: str = "gpt-oss-120b", max_tokens: int = 150, temperature: float = 0.7) -> dict[str,str]:
response = client.chat.completions.create(
# TODO 2: Field in the arguments and the prompt for the Cerebras API
)
# TODO 3: Convert the AI's response message to a JSON string
# TODO 4: Parse the JSON string into a Python dictionary to easily access the 'content' field
# TODO : json.loads(answer_json_str)
return answer_dict
print("Cerebras API Assistant (type 'quit' to exit)")
print("Model: gpt-oss-120b | Max tokens per answer: 150 | Word Length Limit: 8192")
print("-" * 50)
question = input("\nYou: ").strip()
try:
# TODO 5: Use the function ask_cerebras() and print the Bot's reply out
# TODO: print example: "Bot: Hello I am a AI bot"
except Exception as e:
if "429" in str(e):
print("Daily limit or rate limit exceeded. Stopping.")
else:
print(f"Error: {e}")
print("\nGoodbye!")