-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
49 lines (35 loc) · 1.45 KB
/
ai.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
import requests
import json
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
def get_story_prompt(theme, place, prompt):
return f"""
Create a fun and engaging children's story of theme : {theme} {"happening in " + place if place else ""} under 200 words with emojis, markdown for bold, underline, italic with the following details:
{prompt}
Include playful and imaginative elements suitable for children.
Incorporate dialogues to make the story interactive.
Ensure the story has a positive and educational message.
Dont give any note!
"""
def call_llm(prompt):
url = "https://api.groq.com/openai/v1/chat/completions"
payload = {
"messages": [{"role": "user", "content": prompt}],
"model": "llama3-8b-8192",
}
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
response_data = response.json()
print("Mistral AI called")
return response_data["choices"][0]["message"]["content"]
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
return ""
def generate_story(*args):
story_prompt = get_story_prompt(*args)
story = call_llm(story_prompt)
return story_prompt, story