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 websearch with special chars #2537

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion g4f/Provider/Copilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def create_completion(
if prompt is None:
prompt = messages[-1]["content"]
debug.log(f"Copilot: Use conversation: {conversation_id}")
yield Parameters(**{"conversation": conversation.get_dict(), "user": user, "prompt": prompt})

uploaded_images = []
if images is not None:
Expand Down Expand Up @@ -200,6 +199,7 @@ def create_completion(
if not is_started:
raise RuntimeError(f"Invalid response: {last_msg}")
finally:
yield Parameters(**{"conversation": conversation.get_dict(), "user": user, "prompt": prompt})
yield Parameters(**{"cookies": {c.name: c.value for c in session.cookies.jar}})

async def get_access_token_and_cookies(url: str, proxy: str = None, target: str = "ChatAI",):
Expand Down
10 changes: 5 additions & 5 deletions g4f/tools/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import hashlib
from pathlib import Path
from urllib.parse import urlparse
from urllib.parse import urlparse, quote_plus
from datetime import datetime
import datetime
import asyncio
Expand Down Expand Up @@ -177,15 +177,15 @@ async def do_search(prompt: str, query: str = None, instructions: str = DEFAULT_
query = spacy_get_keywords(prompt)
json_bytes = json.dumps({"query": query, **kwargs}, sort_keys=True).encode()
md5_hash = hashlib.md5(json_bytes).hexdigest()
bucket_dir: Path = Path(get_cookies_dir()) / ".scrape_cache" / f"web_search:{datetime.date.today()}"
bucket_dir: Path = Path(get_cookies_dir()) / ".scrape_cache" / f"web_search" / f"{datetime.date.today()}"
bucket_dir.mkdir(parents=True, exist_ok=True)
cache_file = bucket_dir / f"{query[:20]}.{md5_hash}.txt"
cache_file = bucket_dir / f"{quote_plus(query[:20])}.{md5_hash}.txt"
if cache_file.exists():
with open(cache_file, "r") as f:
with cache_file.open("r") as f:
search_results = f.read()
else:
search_results = await search(query, **kwargs)
with open(cache_file, "w") as f:
with cache_file.open("w") as f:
f.write(str(search_results))

new_prompt = f"""
Expand Down
Loading