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 · 3 comments
Open

Enabling web search mode in g4f 0.3.9.6 version #2496

DeveleplorAbdullahH opened this issue Dec 18, 2024 · 3 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.

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