This commit is contained in:
恍兮惚兮 2024-08-13 01:31:33 +08:00
parent d01013b6af
commit e8823fa2b3
2 changed files with 34 additions and 20 deletions

View File

@ -31,19 +31,14 @@ class TS(basetrans):
def translate(self, query): def translate(self, query):
self.checkempty(["SECRET_KEY", "model"]) self.checkempty(["SECRET_KEY", "model"])
self.contextnum = int(self.config["context"]) self.contextnum = int(self.config["context"])
try:
gen_config = { gen_config = {
"generationConfig": { "generationConfig": {
"stopSequences": [" \n"], "stopSequences": [" \n"],
"temperature": self.config["Temperature"], "temperature": self.config["Temperature"],
}
} }
except: }
gen_config = {"generationConfig": {"temperature": 0.3}} model = self.config["model"]
try:
model = self.config["model"]
except:
model = "gemini-1.5-flash"
user_prompt = ( user_prompt = (
self.config.get("user_user_prompt", "") self.config.get("user_user_prompt", "")
if self.config.get("use_user_user_prompt", False) if self.config.get("use_user_user_prompt", False)
@ -103,17 +98,30 @@ class TS(basetrans):
message.append({"role": "user", "parts": [{"text": query}]}) message.append({"role": "user", "parts": [{"text": query}]})
contents = dict(contents=message) contents = dict(contents=message)
usingstream = self.config["usingstream"]
payload = {**contents, **safety, **sys_message, **gen_config} payload = {**contents, **safety, **sys_message, **gen_config}
res = self.proxysession.post( res = self.proxysession.post(
f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent", f"https://generativelanguage.googleapis.com/v1beta/models/{model}:{['generateContent','streamGenerateContent'][usingstream]}",
params={"key": self.multiapikeycurrent["SECRET_KEY"]}, params={"key": self.multiapikeycurrent["SECRET_KEY"]},
json=payload, json=payload,
stream=usingstream,
) )
try: if usingstream:
line = res.json()["candidates"][0]["content"]["parts"][0]["text"] line = ""
except: for __x in res.iter_lines(decode_unicode=True):
raise Exception(res.text) __x = __x.strip()
yield line if not __x.startswith('"text":'):
continue
__x = __x[7:]
__x = eval(__x)
yield __x
line += __x
else:
try:
line = res.json()["candidates"][0]["content"]["parts"][0]["text"]
except:
raise Exception(res.text)
yield line
self.context.append({"role": "user", "parts": [{"text": query}]}) self.context.append({"role": "user", "parts": [{"text": query}]})
self.context.append({"role": "model", "parts": [{"text": line}]}) self.context.append({"role": "model", "parts": [{"text": line}]})

View File

@ -974,7 +974,8 @@
"use_user_user_prompt": false, "use_user_user_prompt": false,
"other_args": "{}", "other_args": "{}",
"use_other_args": false, "use_other_args": false,
"s": "" "s": "",
"usingstream": true
}, },
"argstype": { "argstype": {
"other_args": { "other_args": {
@ -982,6 +983,11 @@
"refswitch": "use_other_args", "refswitch": "use_other_args",
"name": "其他参数" "name": "其他参数"
}, },
"usingstream": {
"name": "流式输出",
"type": "switch",
"rank": 4
},
"user_user_prompt": { "user_user_prompt": {
"name": "user prompt", "name": "user prompt",
"refswitch": "use_user_user_prompt", "refswitch": "use_user_user_prompt",