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

Enabling web search mode in g4f 0.3.9.6 version #2496

Open
DeveleplorAbdullahH opened this issue Dec 18, 2024 · 6 comments
Open

Enabling web search mode in g4f 0.3.9.6 version #2496

DeveleplorAbdullahH opened this issue Dec 18, 2024 · 6 comments

Comments

@DeveleplorAbdullahH
Copy link

How can i enable web search mode in chat completions part like in the GUI? For example, I want with Airforce provider. Can someone please help me? write the full code please with airforce provider

@DeveleplorAbdullahH
Copy link
Author

@xtekky @hlohaus

@hlohaus
Copy link
Collaborator

hlohaus commented Dec 18, 2024

You can include it this way:

            from g4f.gui.server.internet import get_search_message
            messages[-1]["content"] = get_search_message(messages[-1]["content"])

The get_search_message function looks like this:

def get_search_message(prompt, n_results: int = 5, max_words: int = 2500) -> str:
    try:
        search_results = asyncio.run(search(prompt, n_results, max_words))
        message = f"""
{search_results}


Instruction: Using the provided web search results, to write a comprehensive reply to the user request.
Make sure to add the sources of cites using [[Number]](Url) notation after the reference. Example: [[0]](http://google.com)

User request:
{prompt}
"""
        debug.log(f"Web search: '{prompt.strip()[:50]}...' {search_results.used_words} Words")
        return message
    except Exception as e:
        debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}")
        return prompt

@DeveleplorAbdullahH

@blacsheep
Copy link

perhaps in this way, but I guess the web search function is still not working?

from g4f.client import Client
from g4f.Provider import Airforce,RetryProvider
from g4f.gui.server.internet import get_search_message


client = Client(provider=RetryProvider([Airforce]))
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": get_search_message("what day is it today?")}],
    web_search=True
)
print(response.choices[0].message.content)
# reply
# I'm sorry, but I can't provide real-time information such as the current date. Please check your device or a calendar to find out today's date.

Copy link

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

@github-actions github-actions bot added the stale label Dec 27, 2024
@hlohaus
Copy link
Collaborator

hlohaus commented Dec 27, 2024

Please enable debug mode and review the logs. The following code snippet should facilitate this:

import g4f.debug
g4f.debug.logging = True

@blacsheep

@github-actions github-actions bot removed the stale label Dec 28, 2024
@blacsheep
Copy link

Fixed, many thanks. The error was raised by missing package 'duckduckgo'.

Run: pip install -U "g4f[all]" for a fix.

Code snippet:

from g4f.client import Client
from g4f.Provider import Airforce,RetryProvider
from g4f.gui.server.internet import get_search_message


client = Client(provider=RetryProvider([Airforce]))
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": get_search_message("What's the date?")}],
    web_search=True
)
print(response.choices[0].message.content)
# reply
# The date today is Saturday, December 28, 2024. It is the 363rd day of the year, which falls in the 52nd week of the year. Only 2 days remain until the year 2025 begins. The current month, December, is the 12th month of the year [[0]](https://www.calendardate.com/todays.htm) [[1]](https://todays-date.net/) [[3]](https://www.datetoday.net/).

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

No branches or pull requests

3 participants