Spaces:
Sleeping
Sleeping
File size: 1,953 Bytes
55dd1f8 76a8149 55dd1f8 76a8149 55dd1f8 76a8149 | 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 | import os
import json
import itertools
from groq import Groq
from dotenv import load_dotenv
from pymongo import MongoClient
import certifi
import random
load_dotenv()
def get_random():
lst = ["R1","R2","R3","S1","RD1"]
i = random.randint(0,4)
return lst[i]
some = get_random()
GROQ_API_KEY = os.getenv(f"GROQ_API_KEY{some}")
# GROQ_API_KEY = os.getenv("GROQ_API_KEY")
def get_mongo_collection():
CONNECTION_STRING = os.getenv("CONNECTION_STRING")
DB_NAME = os.getenv("DB_NAME")
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
try:
# Connect with certifi to avoid SSL errors
client = MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
db = client[DB_NAME]
return db[COLLECTION_NAME]
except Exception as e:
print(f"Error connecting to Mongo: {e}")
return None
def get_file_data(filepath):
if(filepath.endswith(".json")):
with open(filepath,"r") as f:
data = json.load(f)
return data
else:
with open(filepath,"r") as f:
data = f.read()
return data
def groq_calls(prompt):
client = Groq(api_key=GROQ_API_KEY)
groq_card = client.chat.completions.create(
# 401629
messages=[
{
"role": "user",
"content": f"{prompt}",
},
{
"role":"system",
"content":"You are a psychologist. Output JSON"
}
],
model="openai/gpt-oss-20b",
response_format={"type": "json_object"}
)
# print(groq_card.choices[0].message.content)
return groq_card.choices[0].message.content
def write_to_file(output_filename,groq_response):
# output_filename = "overall_specific_problempair.json"
parsed_json = json.loads(groq_response)
with open(output_filename, 'a', encoding='utf-8') as f:
json.dump(parsed_json, f)
|