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

Bot does not invoke pydantic_ai tools when using gpt4free #2757

Open
grisha765 opened this issue Feb 25, 2025 · 3 comments
Open

Bot does not invoke pydantic_ai tools when using gpt4free #2757

grisha765 opened this issue Feb 25, 2025 · 3 comments
Labels
enhancement New feature or request respond

Comments

@grisha765
Copy link

grisha765 commented Feb 25, 2025

Is your feature request related to a problem? Please describe.
I'm facing an issue where the bot does not use any tools from pydantic_ai. Specifically, the bot should return a True or False response through the selection function, but it's not working with gpt4free. However, when I switch to GeminiModel (from pydantic_ai.models.gemini import GeminiModel), it works as expected. I'd like the bot to correctly call and use the selection tool from pydantic_ai so that the boolean response is accurately reflected.

Describe the solution you'd like
I want the bot to consistently utilize the selection function within pydantic_ai (i.e., selection(bool(False)) or selection(bool(True))) regardless of which model is used. Essentially, the response should explicitly be False or True based on the filter logic, matching the code’s intention.

Describe alternatives you've considered

  • Switching to GeminiModel from pydantic_ai.models.gemini does resolve the issue, but I would prefer to use gpt4free for my setup.
  • Manually forcing the bot to return a hardcoded False or True value is a temporary workaround, but it defeats the purpose of using the agent’s logic.
  • I have tried multiple models offered by gpt4free, but the result remains the same (the selection function is not invoked).

Additional context
Below is the relevant code snippet demonstrating the issue. The selection function is not called when using gpt4free, resulting in Selection call: None and an incorrect agent result:

from pydantic_ai import Agent, Tool
from g4f.tools.pydantic_ai import AIModel


class VacancyBot:
    def __init__(self):
        self.model = AIModel("gpt-4o-mini")
        self.agent_selection = None
        self.agent_result = None
        self.filter_phrase = None

    def init_agent(self):
        self.agent = Agent(
            self.model,
            system_prompt=(
                'You will be sent descriptions of the vacancies, your task is to accept or reject the vacancies with the help of `selection` tool, you should make conclusions to accept or reject the vacancy strictly according to the filter phrase.'
                'BE SURE TO USE A TOOL!'
                f'Filter phrase: {self.filter_phrase}'
            ),
            tools=[
                Tool(self.selection),
            ]
        )

    def selection(self, boolean: bool) -> bool:
        '''
        accept or reject the vacancy
        example reject vacancy:
        selection(bool(False))
        '''
        self.agent_selection = boolean
        return boolean

    def set_filter_phrase(self, phrase):
        self.filter_phrase = phrase

    async def run_bot(self, msg):
        result = await self.agent.run(msg)
        self.agent_result = result.data

    def show_selection(self):
        print("Selection call:", self.agent_selection)

    def show_agent_result(self):
        print("Agent result:", self.agent_result)

if __name__ == '__main__':
    import asyncio
    bot = VacancyBot()
    msg = "you will write programs in Rust"
    bot.set_filter_phrase('reject jobs related to python development, accept all others.')
    bot.init_agent()
    asyncio.run(bot.run_bot(msg))
    bot.show_selection()
    bot.show_agent_result()
python3 gpt_test.py
Selection call: None
Agent result: Accept

With GeminiModel, the bot correctly responds with a boolean value, but with gpt4free, the Selection call remains None and the agent result is incorrect. Any guidance or fix for this behavior would be greatly appreciated.

@hlohaus
Copy link
Collaborator

hlohaus commented Feb 26, 2025

Please refer to the documentation's example. Furthermore, the documentation clearly states that tool support is not yet available.

@hlohaus hlohaus added enhancement New feature or request respond labels Feb 27, 2025
@hlohaus
Copy link
Collaborator

hlohaus commented Mar 2, 2025

@grisha765 I have added a SupportToolProvider:

https://github.com/xtekky/gpt4free/blob/main/docs/pydantic_ai.md#example-for-modelsproviders-without-tool-support-single-tool-usage

@grisha765
Copy link
Author

@grisha765 I have added a SupportToolProvider:

https://github.com/xtekky/gpt4free/blob/main/docs/pydantic_ai.md#example-for-modelsproviders-without-tool-support-single-tool-usage

Thank you, I was able to make it happen.

While this doesn't completely solve the problem, it does work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request respond
Projects
None yet
Development

No branches or pull requests

2 participants