refry proofofwork

This commit is contained in:
LanQian 2024-05-21 06:42:33 +08:00
parent f9698e2179
commit 478d55cfb8
7 changed files with 12 additions and 7 deletions

View File

@ -35,7 +35,7 @@ jobs:
images: lanqian528/chat2api
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=v1.0.7
type=raw,value=v1.0.8
- name: Build and push
uses: docker/build-push-action@v5

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.env
/.idea/
*.pyc
/tmp/

2
app.py
View File

@ -29,8 +29,8 @@ app.add_middleware(
async def to_send_conversation(request_data, access_token):
chat_service = ChatService(access_token)
await chat_service.set_dynamic_data(request_data)
try:
await chat_service.set_dynamic_data(request_data)
await chat_service.get_chat_requirements()
return chat_service
except HTTPException as e:

View File

@ -124,7 +124,9 @@ class ChatService:
if proofofwork_diff <= pow_difficulty:
raise HTTPException(status_code=403, detail=f"Proof of work difficulty too high: {proofofwork_diff}")
proofofwork_seed = proofofwork.get("seed")
self.proof_token = await run_in_threadpool(get_answer_token, proofofwork_seed, proofofwork_diff, config)
self.proof_token, solved = await run_in_threadpool(get_answer_token, proofofwork_seed, proofofwork_diff, config)
if not solved:
raise HTTPException(status_code=403, detail="Failed to solve proof of work")
arkose_required = arkose.get('required')
if arkose_required:
@ -154,7 +156,7 @@ class ChatService:
self.chat_token = resp.get('token')
if not self.chat_token:
raise HTTPException(status_code=502, detail=f"Failed to get chat token: {r.text}")
raise HTTPException(status_code=403, detail=f"Failed to get chat token: {r.text}")
return self.chat_token
else:
if "application/json" == r.headers.get("Content-Type", ""):

View File

@ -343,7 +343,7 @@ def get_answer_token(seed, diff, config):
answer, solved = generate_answer(seed, diff, config)
end = time.time()
logger.info(f'diff: {diff}, time: {int((end - start) * 1e6) / 1e3}ms, solved: {solved}')
return "gAAAAAB" + answer
return "gAAAAAB" + answer, solved
def generate_answer(seed, diff, config):

View File

@ -35,7 +35,7 @@ arkose_token_url_list = arkose_token_url.split(',') if arkose_token_url else []
proxy_url_list = proxy_url.split(',') if proxy_url else []
logger.info("-" * 60)
logger.info("Chat2Api v1.0.7 | https://github.com/lanqian528/chat2api")
logger.info("Chat2Api v1.0.8 | https://github.com/lanqian528/chat2api")
logger.info("-" * 60)
logger.info("Environment variables:")
logger.info("API_PREFIX: " + str(api_prefix))

View File

@ -11,8 +11,9 @@ async def async_retry(func, *args, max_retries=retry_times, **kwargs):
return result
except HTTPException as e:
if attempt == max_retries:
logger.error(f"Throw an exception {e.status_code}, {e.detail}")
raise HTTPException(status_code=e.status_code, detail=e.detail)
logger.error(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")
logger.info(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")
def retry(func, *args, max_retries=retry_times, **kwargs):
@ -22,5 +23,6 @@ def retry(func, *args, max_retries=retry_times, **kwargs):
return result
except HTTPException as e:
if attempt == max_retries:
logger.error(f"Throw an exception {e.status_code}, {e.detail}")
raise HTTPException(status_code=e.status_code, detail=e.detail)
logger.error(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")