First, make sure you have the Openai Python package installed. You can install it using pip:
pip install openai
Now, you can use the following Python code:
import openai
# Set your OpenAI API key here
api_key = 'YOUR_OPENAI_API_KEY'
openai.api_key = api_key
def chat_with_gpt(prompt):
try:
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=50
)
return response['choices'][0]['text'].strip()
except Exception as e:
return f"Error: {e}"
def main():
print("Welcome to ChatGPT 4.0 virtual agent!")
print("You can start chatting. Enter 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.lower() == 'exit':
print("Goodbye!")
break
prompt = f"You: {user_input}\nChatGPT: "
response = chat_with_gpt(prompt)
print("ChatGPT:", response)
if __name__ == "__main__":
main()
To use this script, run it in your terminal or command prompt:
python chatgpt_virtual_agent.py
.png)
Comments
Post a Comment