-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (54 loc) · 2.18 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import base64
import json
# OpenAI API configuration
OPENAI_API_KEY = ""
HEADERS = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json"
}
# Simulated "encryption" and "decryption" functions
def simple_encrypt(text):
# Simulating encryption by encoding text to base64
return base64.b64encode(text.encode('utf-8')).decode('utf-8')
def simple_decrypt(text):
# Simulating decryption by decoding text from base64
return base64.b64decode(text.encode('utf-8')).decode('utf-8')
# Function to send the "encrypted" prompt to OpenAI and receive a response
def send_to_openai(encrypted_prompt):
url = "https://api.openai.com/v1/chat/completions"
# Decoding the prompt for demonstration purposes before sending
decrypted_prompt = simple_decrypt(encrypted_prompt)
# The data payload for the API request
data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": decrypted_prompt}
]
}
# Making the API request
response = requests.post(url, headers=HEADERS, data=json.dumps(data))
if response.status_code == 200:
# Extracting the response content
response_data = response.json()
if 'choices' in response_data and response_data['choices']:
# Assuming only one choice is returned and extracting the content
message_content = response_data['choices'][0]['message']['content']
return message_content
else:
return "No response generated."
else:
print("Failed to get response from OpenAI:", response.text)
return None
# Main workflow function
def full_workflow(user_prompt):
# Simulating encryption of the user prompt
encrypted_prompt = simple_encrypt(user_prompt)
print(f"Encrypted Prompt: {encrypted_prompt}")
# Sending the "encrypted" prompt to OpenAI and getting the response
response = send_to_openai(encrypted_prompt)
print("OpenAI Response:", response)
# Example usage
user_prompt = "List 5 ways to build out a resilient supply chain."
full_workflow(user_prompt)