翻译--添加连接失败时的重试

This commit is contained in:
2024-07-22 15:54:11 +08:00
parent 16c05c0f5b
commit ff971d50fc
4 changed files with 13 additions and 14 deletions

View File

@@ -2,6 +2,8 @@ import json
from typing import Dict, Any
from dashscope import Generation
from retry import retry
from urllib3.exceptions import NewConnectionError
from app.core.config import *
from app.service.chat_robot.script.callbacks.qwen_callback_handler import QWenCallbackHandler
@@ -107,8 +109,7 @@ qwen = QWenCallbackHandler()
def search_from_internet(message):
response = Generation.call(
model='qwen-turbo',
# api_key='sk-7658298c6b99443c98184a5e634fe6ab',
api_key='sk-a6bdf594e1f54a4aa3e9d4d48f8c661f',
api_key=QWEN_API_KEY,
messages=message,
tools=tools,
# seed=random.randint(1, 10000), # 设置随机数种子seed如果没有设置则随机数种子默认为1234
@@ -126,12 +127,11 @@ def get_table_info(table_names):
def query_database(sql_string):
return CustomDatabase.run(db, sql_string)
@retry(exceptions=NewConnectionError, tries=3, delay=1)
def get_response(messages):
response = Generation.call(
model='qwen-max',
# api_key='sk-7658298c6b99443c98184a5e634fe6ab',
api_key='sk-a6bdf594e1f54a4aa3e9d4d48f8c661f',
api_key=QWEN_API_KEY,
messages=messages,
tools=tools,
# seed=random.randint(1, 10000), # 设置随机数种子seed如果没有设置则随机数种子默认为1234

View File

@@ -1,13 +1,11 @@
import logging
from dashscope import Generation
# from langchain.chains import LLMChain
from langchain_community.chat_models import QianfanChatEndpoint, ChatTongyi
# from langchain.chat_models import ChatOpenAI
from langchain_core.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.runnables import RunnableSequence
from requests import RequestException
from retry import retry
from app.core.config import QWEN_API_KEY
from app.core.config import OPENAI_MODEL, OPENAI_API_KEY
# os.environ["http_proxy"] = "http://127.0.0.1:7890"
# os.environ["https_proxy"] = "http://127.0.0.1:7890"
@@ -44,12 +42,11 @@ def translate_to_en(text):
return assistant_output.content
@retry(exceptions=RequestException, tries=3, delay=1)
def get_response(messages):
response = Generation.call(
model='qwen-max',
# api_key='sk-7658298c6b99443c98184a5e634fe6ab',
api_key='sk-a6bdf594e1f54a4aa3e9d4d48f8c661f',
api_key= QWEN_API_KEY,
messages=messages,
# seed=random.randint(1, 10000), # 设置随机数种子seed如果没有设置则随机数种子默认为1234
result_format='message', # 将输出设置为message形式