-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputtoResponse.py
101 lines (69 loc) · 2.83 KB
/
InputtoResponse.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import openai
# -------------------------------------------------------------------
# Just Talk Block
# Set your OpenAI API key here
api_key = "YOUR_OPENAI_API_KEY"
# Initialize the OpenAI API client
openai.api_key = api_key
def generate_response(prompt, model_name="gpt-4"):
try:
response = openai.ChatCompletion.create(
model=model_name,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
return response['choices'][0]['message']['content'].strip()
except openai.error.InvalidRequestError:
if model_name != "gpt-3.5-turbo":
print("Failed to access GPT-4, falling back to GPT-3.5-turbo.")
return generate_response(prompt, "gpt-3.5-turbo")
else:
raise
while True:
# Get user input
input_text = input("Enter your input text (or type 'exit' to quit): ")
if input_text.lower() == "exit":
print("Goodbye!")
break
# Generate a response
generated_response = generate_response(input_text)
# Print the generated response
print("Generated Response:")
print(generated_response)
# -------------------------------------------------------------------------
# Talk With Fixed Prompt Block
# # Set your OpenAI API key here
# api_key = "YOUR_OPENAI_API_KEY"
# # Initialize the OpenAI API client
# openai.api_key = api_key
# # Fixed prompt that will be added to the beginning of each input
# fixed_prompt = "You are acting as a Canadian fratboy. Please phrase your response as such using 'Bro' and 'eh' frequently.\n"
# def generate_response(prompt, model_name="gpt-4"):
# try:
# response = openai.ChatCompletion.create(
# model=model_name,
# messages=[
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": prompt}
# ]
# )
# return response['choices'][0]['message']['content'].strip()
# except openai.error.InvalidRequestError:
# if model_name != "gpt-3.5-turbo":
# print("Failed to access GPT-4, falling back to GPT-3.5-turbo.")
# return generate_response(prompt, "gpt-3.5-turbo")
# else:
# raise
# while True:
# # Get user input
# input_text = input("Enter your input text (or type 'exit' to quit): ")
# if input_text.lower() == "exit":
# print("Goodbye!")
# break
# # Generate a response
# generated_response = generate_response(input_text)
# # Print the generated response
# print("Generated Response:")
# print(generated_response)