SalmanR/Say_It_Professionally
0
1import openai2import os3from dotenv import load_dotenv4from pymongo.mongo_client import MongoClient5 6 7load_dotenv()8openai.api_key=os.getenv("OPENAI_API_KEY")9MongoDB =os.getenv("MongoDB_API")10uri = "mongodb+srv://sipaiemail:{0}@sipai.yjbydw5.mongodb.net/?retryWrites=true&w=majority&ssl=true".format(MongoDB)11 12 13client = MongoClient(uri)14db = client['mydatabase']15collection = db['usage_count']16 17try:18 client.admin.command('ping')19 print("Pinged your deployment. You successfully connected to MongoDB!")20except Exception as e:21 print(e)22 23def update_and_retrieve_counter(update=False):24 if update:25 collection.update_one({'function': 'process_prompt'}, {'$inc': {'count': 1}}, upsert=True)26 current_count = collection.find_one({'function': 'process_prompt'})['count']27 return current_count28 29 30def process_prompt(prompt):31 response = openai.chat.completions.create(32 model="gpt-3.5-turbo-1106",33 response_format={"type": "json_object"},34 messages=[35 {36 "role": "system", 37 "content": "You are a helpful assistant who helps write professional emails designed to output in JSON."38 },39 {40 "role": "user",41 "content": prompt42 }43 ]44 )45 update_and_retrieve_counter(update=True)46 return response.choices[0].message.content