-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
594 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
OPENAI_EMAIL="<YOUR_OPENAI_EMAIL>" | ||
OPENAI_PASSWORD="<YOUR_OPENAI_PASSWORD>" | ||
TELEGRAM_BOT_TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>" | ||
ALLOWED_TELEGRAM_USER_IDS="<USER_ID_1>,<USER_ID_2>,..." | ||
OPENAI_API_KEY="XXX" | ||
TELEGRAM_BOT_TOKEN="XXX" | ||
ALLOWED_TELEGRAM_USER_IDS="USER_ID_1,<USER_ID_2" # comma separated list of telegram user ids, or * to allow all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
import openai | ||
|
||
|
||
class GPTHelper: | ||
""" | ||
ChatGPT helper class. | ||
""" | ||
|
||
def __init__(self, config: dict): | ||
""" | ||
Initializes the GPT helper class with the given configuration. | ||
:param config: A dictionary containing the GPT configuration | ||
""" | ||
openai.api_key = config['api_key'] | ||
self.prompt = "You are a helpful assistant. You answer with concise, straight-forward answers. You sometimes " \ | ||
"make jokes, if appropriate. You are never rude. You are always helpful." | ||
self.history = [{"role": "system", "content": self.prompt}] | ||
|
||
def get_response(self, query) -> str: | ||
""" | ||
Gets a response from the GPT-3 model. | ||
:param query: The query to send to the model | ||
:return: The answer from the model | ||
""" | ||
try: | ||
self.history.append({"role": "user", "content": query}) | ||
|
||
response = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=self.history | ||
) | ||
|
||
answer = response.choices[0]['message']['content'] | ||
self.history.append({"role": "assistant", "content": answer}) | ||
return answer | ||
except openai.error.RateLimitError as e: | ||
logging.exception(e) | ||
return "OpenAI RateLimit exceed" | ||
except Exception as e: | ||
logging.exception(e) | ||
return "Error" | ||
|
||
def reset(self): | ||
""" | ||
Resets the conversation history. | ||
""" | ||
self.history = [{"role": "system", "content": self.prompt}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters