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

Abstract AiHandler to BaseAiHandler #514

Merged
merged 26 commits into from
Dec 14, 2023

Conversation

brianpham93
Copy link
Contributor

In many organization, accessing directly to LLM provider is not permit. By abstracting implementation of AiHandler to an interface, this allows developer to have more freedom in customizing how each function could work with organization's own way of integrating with LLM

@brianpham93
Copy link
Contributor Author

/review

Copy link
Contributor

github-actions bot commented Dec 9, 2023

PR Analysis

(review updated until commit e37598f)

  • 🎯 Main theme: Refactoring and abstraction of AI handlers
  • 📝 PR summary: The PR introduces a new abstraction layer for AI handlers, allowing for more flexibility in integrating with different AI providers. It also includes the implementation of two new AI handlers: OpenAIHandler and LangChainOpenAIHandler. The PR also modifies various tools and servers to use the new AI handler abstraction.
  • 📌 Type of PR: Enhancement
  • 🧪 Relevant tests added: No
  • ⏱️ Estimated effort to review [1-5]: 4, because the PR involves significant changes to the AI handling logic and affects multiple files. It also introduces new AI handlers which need to be reviewed for correctness and efficiency.
  • 🔒 Security concerns: No security concerns found

PR Feedback

  • 💡 General suggestions: The PR is well-structured and the changes are logically grouped. The abstraction of AI handlers is a good step towards making the system more flexible and adaptable. However, it would be beneficial to add tests to ensure that the new handlers and the modified logic work as expected. Additionally, error handling could be improved in some places, and some code could be made more efficient.

  • 🤖 Code feedback:
    relevant filepr_agent/agent/pr_agent.py
    suggestion      It's good to see the use of Python's built-in `inspect` module to check if a class constructor has a specific parameter. However, this approach might not be the most efficient or reliable. A more robust approach could be to use a common interface or base class for all classes that require an `ai_handler`, and then simply check if the class is a subclass of this base class. This would make the code more readable and less prone to errors. [important]
    relevant linedef has_ai_handler_param(cls: object):

    relevant filepr_agent/algo/ai_handlers/openai_ai_handler.py
    suggestion      The `chat_completion` method includes a retry mechanism which is a good practice. However, the retry mechanism could be improved by adding a maximum delay limit to avoid very long delays in case of persistent errors. [medium]
    relevant line@retry(exceptions=(APIError, Timeout, TryAgain, AttributeError, RateLimitError),

    relevant filepr_agent/algo/ai_handlers/langchain_ai_handler.py
    suggestion      The `chat_completion` method seems to be missing error handling for the `self.chat` call. It would be good to add a try-except block around this call to handle potential exceptions and provide a more informative error message. [important]
    relevant lineresp = self.chat(messages, model=model, temperature=temperature)

    relevant filepr_agent/algo/ai_handlers/base_ai_handler.py
    suggestion      The `BaseAiHandler` class is a good start for an AI handler interface. However, it could be improved by adding type hints to the method signatures. This would make the code more readable and would help developers understand what types of arguments are expected. [medium]
    relevant lineasync def chat_completion(self, model: str, system: str, user: str, temperature: float = 0.2):

How to use

Instructions

To invoke the PR-Agent, add a comment using one of the following commands:
/review: Request a review of your Pull Request.
/describe: Update the PR title and description based on the contents of the PR.
/improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
/ask <QUESTION>: Ask a question about the PR.
/update_changelog: Update the changelog based on the PR's contents.
/add_docs: Generate docstring for new components introduced in the PR.
/generate_labels: Generate labels for the PR based on the PR's contents.
see the tools guide for more details.

To edit any configuration parameter from the configuration.toml, add --config_path=new_value.
For example: /review --pr_reviewer.extra_instructions="focus on the file: ..."
To list the possible configuration parameters, add a /config comment.

@brianpham93
Copy link
Contributor Author

/update_changelog

Copy link
Contributor

github-actions bot commented Dec 9, 2023

Changelog updates:

2023-12-09

Changed

  • Abstracted AiHandler to BaseAiHandler to allow more flexibility in integrating with different LLM providers.
  • Updated various tools to accept an instance of BaseAiHandler, enhancing customization options.

to commit the new content to the CHANGELOG.md file, please type:
'/update_changelog --pr_update_changelog.push_changelog_changes=true'

@brianpham93
Copy link
Contributor Author

/generate_labels

@github-actions github-actions bot added the enhancement New feature or request label Dec 9, 2023
Copy link
Contributor

@barnett-yuxiang barnett-yuxiang left a comment

Choose a reason for hiding this comment

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

LGTM

@Olamyy
Copy link

Olamyy commented Dec 10, 2023

/review

Copy link
Contributor

Persistent review updated to latest commit b640992

@Olamyy
Copy link

Olamyy commented Dec 10, 2023

/auto_review

Copy link
Contributor

Persistent review updated to latest commit b640992

@mrT23
Copy link
Collaborator

mrT23 commented Dec 11, 2023

Hi @brianpham93
The base idea is solid - enabling multiple ai handlers.
However, this PR needs more work and changes before it can merged:

  1. Not every tool was adapted to the new format. Hence, they will crash. This is beyond crucial to fix it fully. Triple-check that there are no instances of AiHandler() remaining, and that every tool still runs successfully.

  2. with only one implementation of BaseAiHandler, it will be hard for other people to understand how to use it. You should provide at least one other implementation. The current AiHandler() should be renamed to LMAiHandler() (LM == LiteLLM), and you need to add another handler - OpenAIHandler(). Its not hard, since they have the same basic interface.
    Make sure you can actually test both handlers, and that they work.

As a side note, in contrast to the PR description, the current handler does not directly access the LLM provider ("In many organization, accessing directly to LLM provider is not permit") - it uses LiteLLM interface wrapper

@brianpham93
Copy link
Contributor Author

thanks @mrT23 . i'll improve this PR further

@mrT23
Copy link
Collaborator

mrT23 commented Dec 11, 2023

Persistent review updated to latest commit b640992

1 similar comment
@mrT23
Copy link
Collaborator

mrT23 commented Dec 11, 2023

Persistent review updated to latest commit b640992

method

This commit refactors the PRAgent class and the has_ai_handler_param
method. The has_ai_handler_param method is moved outside the class and
made a standalone function. This change improves code organization and
readability. The has_ai_handler_param function now takes a class object
as a parameter and checks if the class constructor has an "ai_handler"
parameter. This refactoring is done to streamline the code and improve
its maintainability.

No issue references.
@brianpham93
Copy link
Contributor Author

/review

@brianpham93
Copy link
Contributor Author

/update_changelog

Copy link
Contributor

Changelog updates:

2023-12-13

Changed

  • Abstracted AiHandler to BaseAiHandler to allow more flexibility in AI handling.
  • Refactored AI handler classes and updated their instantiation in server files.
  • Moved AI handlers to a specific folder for better organization.

Added

  • Introduced OpenAIHandler and LangChainOpenAIHandler as new AI handlers.
  • Added functionality to load default AI Handler from a util function.
  • Added logging to pr_agent.py and code documentation generation for PR diffs.

to commit the new content to the CHANGELOG.md file, please type:
'/update_changelog --pr_update_changelog.push_changelog_changes=true'

Copy link
Contributor

Persistent review updated to latest commit e37598f

@mrT23
Copy link
Collaborator

mrT23 commented Dec 14, 2023

Hi @brianpham93 , good work.
i tested 'LangChainOpenAIHandler' and 'OpenAIHandler' (in addition to 'LiteLLMAIHandler') and they all worked.

I hade some issues with the structure and where to give the default values. In general, I don't recommend initializing things on the main path

I made some corrections, simplifications and protections, nothing that changes the behavior. Please merge this PR to yours:
#525

I did move the 'LangChainOpenAIHandler' to be optional, since this is quite an heavy installation, and currently I don't see an advantage it has over plain OpenAIHandler. if you think otherwise, let me know

after you merge my PR to yours, we can merge to main

@brianpham93
Copy link
Contributor Author

thank @mrT23 , i've merged your PR's changes to my branch

@mrT23
Copy link
Collaborator

mrT23 commented Dec 14, 2023

great work @brianpham93

@mrT23 mrT23 merged commit 54891ad into Codium-ai:main Dec 14, 2023
5 checks passed
yochail pushed a commit to yochail/pr-agent that referenced this pull request Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants