This repository has been archived on 2025-09-28. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
ChatGPT-PyAPI/example.py

32 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2023-03-09 16:55:18 +01:00
# Copyright (c) 2023 Julian Müller (ChaoticByte)
from os import environ
from chatgpt_pyapi import ChatGPT
from chatgpt_pyapi import Message
from chatgpt_pyapi import Models
from chatgpt_pyapi import Roles
2023-03-09 17:26:35 +01:00
# Read the API key from a environment variable
API_KEY = environ["OPENAI_API_KEY"]
if __name__ == "__main__":
2023-03-09 17:26:35 +01:00
# Create ChatGPT API instance
cgpt = ChatGPT(API_KEY, model=Models.GPT_35_TURBO_0301)
2023-03-09 17:26:35 +01:00
# Provide a system message that will influence the answers
system_in = "Please provide the following answers as cynical as possible, but still correct."
2023-03-09 17:26:35 +01:00
# Add the message to the history, but don't send it yet
cgpt.add_to_chat(Message(system_in, role=Roles.SYSTEM))
2023-03-09 17:26:35 +01:00
# Have a little chat :D
user_in = "Who are you?"
print(f"USER: {user_in}")
print(cgpt.chat(Message(user_in)).text)
user_in = "Could you please elaborate?"
print(f"\nUSER: {user_in}")
print(cgpt.chat(Message(user_in)).text)
2023-03-09 17:26:35 +01:00
# Print out message history
print("\nMessage History:")
[print(m.to_api()) for m in cgpt._message_history]