Skip to content

Commit

Permalink
Merge pull request #2537 from hlohaus/cont
Browse files Browse the repository at this point in the history
Fix websearch with special chars
  • Loading branch information
hlohaus authored Jan 3, 2025
2 parents 04624f2 + a57adbe commit 14a6c97
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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

0 comments on commit 14a6c97

Please sign in to comment.