fix: test

This commit is contained in:
yihong0618 2024-03-14 21:02:25 +08:00
parent 6f6771e1e8
commit e5838d7876
4 changed files with 8 additions and 207 deletions

View File

@ -28,6 +28,8 @@ for yihong0618's channel: https://t.me/hyi0618
2. export ANTHROPIC_API_KEY=${the_key} 2. export ANTHROPIC_API_KEY=${the_key}
3. use `claude: ${message}` to ask 3. use `claude: ${message}` to ask
Note, if you are using third party service, you need to `export ANTHROPIC_BASE_URL=${the_url}` to change the url.
## HOW TO Install and Run ## HOW TO Install and Run

View File

@ -1,121 +1,21 @@
from os import environ from os import environ
from pathlib import Path from pathlib import Path
import re
from anthropic import Anthropic, APITimeoutError from anthropic import Anthropic, APITimeoutError
from telebot import TeleBot from telebot import TeleBot
from telebot.types import Message from telebot.types import Message
from md2tgmd import escape
ANTHROPIC_API_KEY = environ.get("ANTHROPIC_API_KEY") ANTHROPIC_API_KEY = environ.get("ANTHROPIC_API_KEY")
ANTHROPIC_MODEL = "claude-3-sonnet-20240229" # change model here you can use claude-3-opus-20240229 but for now its slow ANTHROPIC_BASE_URL = environ.get("ANTHROPIC_BASE_URL")
ANTHROPIC_MODEL = "claude-3-haiku-20240307" # change model here you can use claude-3-opus-20240229 but for now its slow
client = Anthropic(api_key=ANTHROPIC_API_KEY, timeout=20) client = Anthropic(base_url=ANTHROPIC_BASE_URL, api_key=ANTHROPIC_API_KEY, timeout=20)
# Global history cache # Global history cache
claude_player_dict = {} claude_player_dict = {}
#### Utils for claude ####
# Note this code copy from https://github.com/yym68686/md2tgmd/blob/main/src/md2tgmd.py
# great thanks
def find_all_index(str, pattern):
index_list = [0]
for match in re.finditer(pattern, str, re.MULTILINE):
if match.group(1) != None:
start = match.start(1)
end = match.end(1)
index_list += [start, end]
index_list.append(len(str))
return index_list
def replace_all(text, pattern, function):
poslist = [0]
strlist = []
originstr = []
poslist = find_all_index(text, pattern)
for i in range(1, len(poslist[:-1]), 2):
start, end = poslist[i : i + 2]
strlist.append(function(text[start:end]))
for i in range(0, len(poslist), 2):
j, k = poslist[i : i + 2]
originstr.append(text[j:k])
if len(strlist) < len(originstr):
strlist.append("")
else:
originstr.append("")
new_list = [item for pair in zip(originstr, strlist) for item in pair]
return "".join(new_list)
def escapeshape(text):
return "▎*" + text.split()[1] + "*"
def escapeminus(text):
return "\\" + text
def escapebackquote(text):
return r"\`\`"
def escapeplus(text):
return "\\" + text
def escape(text, flag=0):
# In all other places characters
# _ * [ ] ( ) ~ ` > # + - = | { } . !
# must be escaped with the preceding character '\'.
text = re.sub(r"\\\[", "@->@", text)
text = re.sub(r"\\\]", "@<-@", text)
text = re.sub(r"\\\(", "@-->@", text)
text = re.sub(r"\\\)", "@<--@", text)
if flag:
text = re.sub(r"\\\\", "@@@", text)
text = re.sub(r"\\", r"\\\\", text)
if flag:
text = re.sub(r"\@{3}", r"\\\\", text)
text = re.sub(r"_", "\_", text)
text = re.sub(r"\*{2}(.*?)\*{2}", "@@@\\1@@@", text)
text = re.sub(r"\n{1,2}\*\s", "\n\n", text)
text = re.sub(r"\*", "\*", text)
text = re.sub(r"\@{3}(.*?)\@{3}", "*\\1*", text)
text = re.sub(r"\!?\[(.*?)\]\((.*?)\)", "@@@\\1@@@^^^\\2^^^", text)
text = re.sub(r"\[", "\[", text)
text = re.sub(r"\]", "\]", text)
text = re.sub(r"\(", "\(", text)
text = re.sub(r"\)", "\)", text)
text = re.sub(r"\@\-\>\@", "\[", text)
text = re.sub(r"\@\<\-\@", "\]", text)
text = re.sub(r"\@\-\-\>\@", "\(", text)
text = re.sub(r"\@\<\-\-\@", "\)", text)
text = re.sub(r"\@{3}(.*?)\@{3}\^{3}(.*?)\^{3}", "[\\1](\\2)", text)
text = re.sub(r"~", "\~", text)
text = re.sub(r">", "\>", text)
text = replace_all(text, r"(^#+\s.+?$)|```[\D\d\s]+?```", escapeshape)
text = re.sub(r"#", "\#", text)
text = replace_all(
text, r"(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeplus
)
text = re.sub(r"\n{1,2}(\s*)-\s", "\n\n\\1• ", text)
text = re.sub(r"\n{1,2}(\s*\d{1,2}\.\s)", "\n\n\\1", text)
text = replace_all(
text, r"(-)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeminus
)
text = re.sub(r"```([\D\d\s]+?)```", "@@@\\1@@@", text)
text = replace_all(text, r"(``)", escapebackquote)
text = re.sub(r"\@{3}([\D\d\s]+?)\@{3}", "```\\1```", text)
text = re.sub(r"=", "\=", text)
text = re.sub(r"\|", "\|", text)
text = re.sub(r"{", "\{", text)
text = re.sub(r"}", "\}", text)
text = re.sub(r"\.", "\.", text)
text = re.sub(r"!", "\!", text)
return text
def claude_handler(message: Message, bot: TeleBot) -> None: def claude_handler(message: Message, bot: TeleBot) -> None:
"""claude : /claude <question>""" """claude : /claude <question>"""
reply_message = bot.reply_to( reply_message = bot.reply_to(

View File

@ -1,11 +1,11 @@
from os import environ from os import environ
from pathlib import Path
import re import re
import google.generativeai as genai import google.generativeai as genai
from google.generativeai.types.generation_types import StopCandidateException from google.generativeai.types.generation_types import StopCandidateException
from telebot import TeleBot from telebot import TeleBot
from telebot.types import Message from telebot.types import Message
from md2tgmd import escape
GOOGLE_GEMINI_KEY = environ.get("GOOGLE_GEMINI_KEY") GOOGLE_GEMINI_KEY = environ.get("GOOGLE_GEMINI_KEY")
@ -34,107 +34,6 @@ safety_settings = [
gemini_player_dict = {} gemini_player_dict = {}
#### Utils for gemini ####
# Note this code copy from https://github.com/yym68686/md2tgmd/blob/main/src/md2tgmd.py
# great thanks
def find_all_index(str, pattern):
index_list = [0]
for match in re.finditer(pattern, str, re.MULTILINE):
if match.group(1) != None:
start = match.start(1)
end = match.end(1)
index_list += [start, end]
index_list.append(len(str))
return index_list
def replace_all(text, pattern, function):
poslist = [0]
strlist = []
originstr = []
poslist = find_all_index(text, pattern)
for i in range(1, len(poslist[:-1]), 2):
start, end = poslist[i : i + 2]
strlist.append(function(text[start:end]))
for i in range(0, len(poslist), 2):
j, k = poslist[i : i + 2]
originstr.append(text[j:k])
if len(strlist) < len(originstr):
strlist.append("")
else:
originstr.append("")
new_list = [item for pair in zip(originstr, strlist) for item in pair]
return "".join(new_list)
def escapeshape(text):
return "▎*" + text.split()[1] + "*"
def escapeminus(text):
return "\\" + text
def escapebackquote(text):
return r"\`\`"
def escapeplus(text):
return "\\" + text
def escape(text, flag=0):
# In all other places characters
# _ * [ ] ( ) ~ ` > # + - = | { } . !
# must be escaped with the preceding character '\'.
text = re.sub(r"\\\[", "@->@", text)
text = re.sub(r"\\\]", "@<-@", text)
text = re.sub(r"\\\(", "@-->@", text)
text = re.sub(r"\\\)", "@<--@", text)
if flag:
text = re.sub(r"\\\\", "@@@", text)
text = re.sub(r"\\", r"\\\\", text)
if flag:
text = re.sub(r"\@{3}", r"\\\\", text)
text = re.sub(r"_", "\_", text)
text = re.sub(r"\*{2}(.*?)\*{2}", "@@@\\1@@@", text)
text = re.sub(r"\n{1,2}\*\s", "\n\n", text)
text = re.sub(r"\*", "\*", text)
text = re.sub(r"\@{3}(.*?)\@{3}", "*\\1*", text)
text = re.sub(r"\!?\[(.*?)\]\((.*?)\)", "@@@\\1@@@^^^\\2^^^", text)
text = re.sub(r"\[", "\[", text)
text = re.sub(r"\]", "\]", text)
text = re.sub(r"\(", "\(", text)
text = re.sub(r"\)", "\)", text)
text = re.sub(r"\@\-\>\@", "\[", text)
text = re.sub(r"\@\<\-\@", "\]", text)
text = re.sub(r"\@\-\-\>\@", "\(", text)
text = re.sub(r"\@\<\-\-\@", "\)", text)
text = re.sub(r"\@{3}(.*?)\@{3}\^{3}(.*?)\^{3}", "[\\1](\\2)", text)
text = re.sub(r"~", "\~", text)
text = re.sub(r">", "\>", text)
text = replace_all(text, r"(^#+\s.+?$)|```[\D\d\s]+?```", escapeshape)
text = re.sub(r"#", "\#", text)
text = replace_all(
text, r"(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeplus
)
text = re.sub(r"\n{1,2}(\s*)-\s", "\n\n\\1• ", text)
text = re.sub(r"\n{1,2}(\s*\d{1,2}\.\s)", "\n\n\\1", text)
text = replace_all(
text, r"(-)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeminus
)
text = re.sub(r"```([\D\d\s]+?)```", "@@@\\1@@@", text)
text = replace_all(text, r"(``)", escapebackquote)
text = re.sub(r"\@{3}([\D\d\s]+?)\@{3}", "```\\1```", text)
text = re.sub(r"=", "\=", text)
text = re.sub(r"\|", "\|", text)
text = re.sub(r"{", "\{", text)
text = re.sub(r"}", "\}", text)
text = re.sub(r"\.", "\.", text)
text = re.sub(r"!", "\!", text)
return text
def make_new_gemini_convo(): def make_new_gemini_convo():
model = genai.GenerativeModel( model = genai.GenerativeModel(
model_name="gemini-pro", model_name="gemini-pro",

View File

@ -4,4 +4,4 @@ github_poster
prettymapp prettymapp
google-generativeai==0.3.1 google-generativeai==0.3.1
anthropic anthropic
md2tgmd