Advertisement
jarekmor

OpenAI_JSON_Parser

Aug 23rd, 2023
1,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import os
  2. import openai
  3.  
  4. openai.api_key = os.getenv("OPENAI_API_KEY")
  5.  
  6. SYSTEM_PROMPT="""You are a consumer-facing application called [Weather_API]. Your purpose is to take user input and respond with structured JSON data.
  7. It's absolutely crucial that you follow these instructions. Every response should strictly adhere to a JSON format.
  8. If the prompt doesn't make sense, is invalid, unclear, or outside the range of your knowledge or capabilities, respond with an error JSON like this: { \\\"error\\\": \\\"[JSON Error]\\\" }.
  9. Do not engage in general conversation, creative writing or go beyond the scope of the user's prompt. Stick to the instruction and always respond in the defined format.
  10. Use the following JSON output as example for the respond:
  11. {
  12. "morning":{
  13.    "temperature": "2°C",
  14.    "weather": "gentle mists",
  15.    "wind": "breeze"
  16.    },
  17. "afternoon": {
  18.    "temperature": "33°C",
  19.    "weather": "clouds",
  20.    "wind": "light wind"
  21.    },
  22. "evening": {
  23.    "temperature": "27°C",
  24.    "weather": "cloudy",
  25.    "wind": "storm"
  26.    },
  27. "night": {
  28.    "temperature": "14°C",
  29.    "weather": "partly cloudy",
  30.    "wind": "tornado"
  31.    },
  32. }
  33. """
  34.  
  35. USER_PROMPT= """Morning:
  36. Temperatures will start off crisp at 12°C, with gentle mists covering the banks of the Vistula River.
  37. There will be an occasional chirp of a bird, signifying the day's awakening.
  38. A soft northwesterly breeze will rustle the trees.
  39.        
  40. Afternoon:
  41. As midday approaches, expect the sun to break through, lifting the temperatures to a comfortable 23°C.
  42. Puffy cumulus clouds will dot the sky, casting fleeting shadows across Warsaw's historical streets and buildings.
  43. A light breeze will be felt, offering a nice respite from the mild warmth.
  44.  
  45. Evening:
  46. Sunset will paint the sky in hues of purple and pink. The temperature is predicted to drop to 17°C by 9:00 PM.
  47. It's a perfect time to enjoy a quiet evening stroll in Łazienki Park or a meal at an outdoor cafe.
  48. There will be a 20% chance of a brief drizzle later in the night.
  49.  
  50. Night:
  51. Nightfall will see a partly cloudy sky with the gentle glow of the moon illuminating the cityscape. The wind will calm, leaving a serene atmosphere.
  52. Overnight lows will drop to 14°C. It will be a good night to leave the windows slightly ajar and let in the fresh cool air.
  53. """
  54.  
  55. response = openai.ChatCompletion.create(
  56.     model="gpt-3.5-turbo",
  57.     messages=[
  58.     {
  59.         "role": "system",
  60.         "content": f"{SYSTEM_PROMPT}"
  61.     },
  62.     {
  63.         "role": "user",
  64.         "content": f"{USER_PROMPT}"
  65.     }
  66.     ],
  67.     temperature=0.3,
  68.     max_tokens=563,
  69.     top_p=1,
  70.     frequency_penalty=0,
  71.     presence_penalty=0
  72.     )
  73.  
  74. print(response.choices[0].message.content)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement