From 6101c260bfc2aed7316ab831aa1c4a3529cb4190 Mon Sep 17 00:00:00 2001 From: jmbwell Date: Tue, 29 Aug 2023 20:04:23 +0000 Subject: [PATCH] move api key to config.ini --- .gitignore | 1 + plugin.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 68bc17f..377b907 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +config.ini diff --git a/plugin.py b/plugin.py index b429a0d..9b223ae 100644 --- a/plugin.py +++ b/plugin.py @@ -28,6 +28,8 @@ ### +import configparser +import os import random import re import requests @@ -51,6 +53,12 @@ class Chat(callbacks.Plugin): self.__parent = super(Chat, self) self.__parent.__init__(irc) + # Load the API key from the configuration file + config_path = os.path.join(os.path.dirname(__file__), 'config.ini') + self.config = configparser.ConfigParser() + self.config.read(config_path) + self.api_key = self.config.get('Chat', 'api_key') + def conversation_history(self): history = irclib.IrcState.history[-30:] filtered_messages = [(msg.args[0], msg.args[1]) for msg in history if msg.command == 'PRIVMSG'] @@ -73,7 +81,6 @@ class Chat(callbacks.Plugin): str: ChatGPT's response """ - api_key = "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" model = "gpt-3.5-turbo" system_prompt = "I am an IRC bot named magicvoice, in an IRC channel called ##huffaz. Everyone knows I am an AI and what my limitations are. The following is the last 30 messages exchanged in the channel among users, including myself." @@ -87,7 +94,7 @@ class Chat(callbacks.Plugin): res = requests.post(f"https://api.openai.com/v1/chat/completions", headers = { # res = requests.post(f"http://localhost:8000/capture", headers = { "Content-Type":"application/json", - "Authorization":f"Bearer {api_key}" + "Authorization":f"Bearer {self.api_key}" }, json={ "model": model,