Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenaiChat provider, improve proofofwork #1974

Merged
merged 1 commit into from
May 18, 2024
Merged

Conversation

hlohaus
Copy link
Collaborator

@hlohaus hlohaus commented May 18, 2024

No description provided.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Review

Summary

The pull request introduces several changes to the OpenaiChat provider and related files. The main focus seems to be on improving the proof of work mechanism and handling authentication requirements more robustly.

Code Review

  • The addition of max_retries as a parameter in create_async_generator is a good approach to handle transient errors that may occur during API interactions.
  • Refactoring the proof of work and authentication token generation into the retry loop is a smart move, ensuring that each attempt has a fresh set of credentials.
  • The global state change for proofTokens in har_file.py could potentially lead to side effects. Consider encapsulating this state within a class or a context manager for better isolation.
  • The removal of the proof token cache in proofofwork.py simplifies the code, but ensure that this does not impact performance significantly due to the lack of caching.

Suggestions

  • It would be helpful to include comments explaining the rationale behind significant changes, especially for the refactoring of authentication and proof of work handling.
  • Consider adding unit tests to cover the new retry logic and ensure that the proof of work generation behaves as expected under different conditions.

Overall, the changes are well thought out and address some of the complexities involved in managing authentication and proof of work for API interactions. Great work!


Thank you again for your efforts, and looking forward to seeing these improvements in action!

Best,
g4f Copilot

g4f/Provider/needs_auth/OpenaiChat.py Show resolved Hide resolved
g4f/Provider/needs_auth/OpenaiChat.py Show resolved Hide resolved
g4f/Provider/needs_auth/OpenaiChat.py Show resolved Hide resolved
g4f/Provider/needs_auth/OpenaiChat.py Show resolved Hide resolved
g4f/Provider/needs_auth/OpenaiChat.py Show resolved Hide resolved

def generate_proof_token(required: bool, seed: str = None, difficulty: str = None, user_agent: str = None, proofTokens: list = None):
def generate_proof_token(required: bool, seed: str = "", difficulty: str = "", user_agent: str = None, proofTokens: list = None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default values for seed and difficulty have been changed to empty strings, which alters the function's behavior. Ensure this change aligns with the intended use cases.


if proofTokens:
config = random.choice(proofTokens)
config = proofTokens[-1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selecting the last element of proofTokens may not be random as the previous implementation. If randomness is required, consider maintaining that feature.

config[7] = random.randint(101, 2100)

diff_len = None if difficulty is None else len(difficulty)
diff_len = len(difficulty)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will raise a ValueError if difficulty is an empty string, which is now a possible default value due to the change on line 8.

for i in range(100000):
config[3] = i
json_data = json.dumps(config)
base = base64.b64encode(json_data.encode()).decode()
hash_value = hashlib.sha3_512((seed or "" + base).encode()).digest()
hash_value = hashlib.sha3_512((seed + base).encode()).digest()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concatenating seed and base without a delimiter may lead to unexpected hash values. Consider adding a delimiter between them.

return "gAAAAAC" + base
proof_token_cache[seed] = "gAAAAAB" + base
return proof_token_cache[seed]
if hash_value.hex()[:diff_len] <= difficulty:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition hash_value.hex()[:diff_len] <= difficulty assumes difficulty is a hexadecimal string. Validate that difficulty adheres to the expected format.

@hlohaus hlohaus merged commit fcd3647 into xtekky:main May 18, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant