Make ChatGPT base_url configurable (#73)

* Add configurable base_url

* Conditionally exclude a parameter if Gemini is the model

Sorry for the delay on this... The readme and help texts need updating, maybe few tweaks here and there, but obviously I never got around to it personally, and this is still a great addition, so going to just commit as is for now since you were thoughtful enough to make sure it doesn't break anything. Thanks again.
This commit is contained in:
Nelluk 2025-03-13 00:17:49 -04:00 committed by GitHub
parent 54fe2c9dcc
commit 91b1402cb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 14 deletions

View File

@ -74,6 +74,19 @@ conf.registerGlobalValue(
), ),
) )
conf.registerGlobalValue(
ChatGPT,
"base_url",
registry.String(
"https://api.openai.com/v1/",
_(
"""
API server, for using a non-OpenAI model which has a compatible API, default: "https://api.openai.com/v1/"
"""
),
),
)
conf.registerChannelValue( conf.registerChannelValue(
ChatGPT, ChatGPT,
"prompt", "prompt",
@ -100,6 +113,7 @@ conf.registerChannelValue(
), ),
) )
conf.registerChannelValue( conf.registerChannelValue(
ChatGPT, ChatGPT,
"reply_intact", "reply_intact",

View File

@ -32,7 +32,8 @@ from supybot import utils, plugins, ircutils, callbacks
from supybot.commands import * from supybot.commands import *
from supybot.i18n import PluginInternationalization from supybot.i18n import PluginInternationalization
import re import re
import openai #import openai
from openai import OpenAI
_ = PluginInternationalization("ChatGPT") _ = PluginInternationalization("ChatGPT")
@ -57,26 +58,41 @@ class ChatGPT(callbacks.Plugin):
return return
if self.registryValue("nick_include", msg.channel): if self.registryValue("nick_include", msg.channel):
text = "%s: %s" % (msg.nick, text) text = "%s: %s" % (msg.nick, text)
# Initialize client
client = OpenAI(
api_key=self.registryValue("api_key"),
base_url=self.registryValue("base_url")
)
self.history.setdefault(channel, None) self.history.setdefault(channel, None)
max_history = self.registryValue("max_history", msg.channel) max_history = self.registryValue("max_history", msg.channel)
prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick) prompt = self.registryValue("prompt", msg.channel).replace("$botnick", irc.nick)
if not self.history[channel] or max_history < 1: if not self.history[channel] or max_history < 1:
self.history[channel] = [] self.history[channel] = []
openai.api_key = self.registryValue("api_key")
completion = openai.chat.completions.create( model_name = self.registryValue("model", msg.channel)
model=self.registryValue("model", msg.channel),
messages=self.history[channel][-max_history:] # Base request parameters
+ [ request_params = {
"model": model_name,
"messages": self.history[channel][-max_history:] + [
{"role": "system", "content": prompt}, {"role": "system", "content": prompt},
{"role": "user", "content": text}, {"role": "user", "content": text}
], ],
temperature=self.registryValue("temperature", msg.channel), "temperature": self.registryValue("temperature", msg.channel),
top_p=self.registryValue("top_p", msg.channel), "top_p": self.registryValue("top_p", msg.channel),
max_tokens=self.registryValue("max_tokens", msg.channel), "max_tokens": self.registryValue("max_tokens", msg.channel),
presence_penalty=self.registryValue("presence_penalty", msg.channel), "presence_penalty": self.registryValue("presence_penalty", msg.channel),
frequency_penalty=self.registryValue("frequency_penalty", msg.channel), "user": msg.nick,
user=msg.nick, }
)
# Gemini models fail if frequency_penalty is included
if "gemini" not in model_name.lower():
request_params["frequency_penalty"] = self.registryValue("frequency_penalty", msg.channel)
completion = client.chat.completions.create(**request_params)
if self.registryValue("nick_strip", msg.channel): if self.registryValue("nick_strip", msg.channel):
content = re.sub( content = re.sub(
r"^%s: " % (irc.nick), "", completion.choices[0].message.content r"^%s: " % (irc.nick), "", completion.choices[0].message.content