Skip to content

Commit

Permalink
Add websearch to gui
Browse files Browse the repository at this point in the history
Fix version_check config
  • Loading branch information
hlohaus committed Dec 6, 2023
1 parent 5862d55 commit bc856b0
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 114 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/publish-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Setup Buildx
uses: docker/setup-buildx-action@v3
- name: Checkout repository
uses: actions/checkout@v4

- name: Get metadata for Docker
id: metadata
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
images: |
hlohaus789/g4f
ghcr.io/${{ github.repository }}
- name: Log in to Docker Hub
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }}

- name: Build and push image
uses: docker/build-push-action@v5
with:
Expand Down
18 changes: 5 additions & 13 deletions g4f/Provider/MyShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..typing import CreateResult, Messages
from .base_provider import BaseProvider
from .helper import format_prompt
from ..webdriver import WebDriver, WebDriverSession
from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare

class MyShell(BaseProvider):
url = "https://app.myshell.ai/chat"
Expand All @@ -25,16 +25,8 @@ def create_completion(
**kwargs
) -> CreateResult:
with WebDriverSession(webdriver, "", proxy=proxy) as driver:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get(cls.url)

# Wait for page load and cloudflare validation
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "body:not(.no-js)"))
)
bypass_cloudflare(driver, cls.url, timeout)

# Send request with message
data = {
"botId": "4738",
Expand All @@ -58,11 +50,11 @@ def create_completion(
driver.execute_script(script.replace("{body}", json.dumps(data)))
script = """
chunk = await window._reader.read();
if (chunk['done']) {
if (chunk.done) {
return null;
}
content = '';
chunk['value'].split('\\n').forEach((line, index) => {
chunk.value.split('\\n').forEach((line, index) => {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.substring('data: '.length));
Expand Down
66 changes: 66 additions & 0 deletions g4f/Provider/unfinished/AiChatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from __future__ import annotations

from urllib.parse import unquote

from ...typing import AsyncResult, Messages
from ..base_provider import BaseProvider
from ...webdriver import WebDriver
from ...requests import Session, get_session_from_browser

class AiChatting(BaseProvider):
url = "https://www.aichatting.net"
supports_gpt_35_turbo = True
_session: Session = None

@classmethod
def create_completion(
cls,
model: str,
messages: Messages,
stream: bool,
proxy: str = None,
timeout: int = 120,
webdriver: WebDriver = None,
**kwargs
) -> AsyncResult:
if not cls._session:
cls._session = get_session_from_browser(cls.url, webdriver, proxy, timeout)
visitorId = unquote(cls._session.cookies.get("aichatting.website.visitorId"))

headers = {
"accept": "application/json, text/plain, */*",
"lang": "en",
"source": "web"
}
data = {
"roleId": 0,
}
try:
response = cls._session.post("https://aga-api.aichatting.net/aigc/chat/record/conversation/create", json=data, headers=headers)
response.raise_for_status()
conversation_id = response.json()["data"]["conversationId"]
except Exception as e:
cls.reset()
raise e
headers = {
"authority": "aga-api.aichatting.net",
"accept": "text/event-stream,application/json, text/event-stream",
"lang": "en",
"source": "web",
"vtoken": visitorId,
}
data = {
"spaceHandle": True,
"roleId": 0,
"messages": messages,
"conversationId": conversation_id,
}
response = cls._session.post("https://aga-api.aichatting.net/aigc/chat/v2/stream", json=data, headers=headers, stream=True)
response.raise_for_status()
for chunk in response.iter_lines():
if chunk.startswith(b"data:"):
yield chunk[5:].decode().replace("-=- --", " ").replace("-=-n--", "\n").replace("--@DONE@--", "")

@classmethod
def reset(cls):
cls._session = None
3 changes: 2 additions & 1 deletion g4f/Provider/unfinished/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .MikuChat import MikuChat
from .Komo import Komo
from .ChatAiGpt import ChatAiGpt
from .ChatAiGpt import ChatAiGpt
from .AiChatting import AiChatting
14 changes: 6 additions & 8 deletions g4f/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
from .Provider import BaseProvider, AsyncGeneratorProvider, RetryProvider
from .typing import Messages, CreateResult, AsyncResult, Union, List
from . import debug

version = '0.1.9.2'
version_check = True
from importlib.metadata import version

def check_pypi_version() -> None:
try:
response = get("https://pypi.org/pypi/g4f/json").json()
latest_version = response["info"]["version"]

if version != latest_version:
if version('g4f') != latest_version:
print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
return False
return True
Expand All @@ -27,6 +25,9 @@ def get_model_and_provider(model : Union[Model, str],
ignored : List[str] = None,
ignore_working: bool = False,
ignore_stream: bool = False) -> tuple[Model, type[BaseProvider]]:
if debug.version_check:
if check_pypi_version():
debug.version_check = False

if isinstance(model, str):
if model in ModelUtils.convert:
Expand Down Expand Up @@ -118,7 +119,4 @@ def create(model : Union[Model, str],

result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs)

return result if stream else ''.join(result)

if version_check:
check_pypi_version()
return result if stream else ''.join(result)
3 changes: 2 additions & 1 deletion g4f/debug.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
logging = False
logging = False
version_check = True
11 changes: 6 additions & 5 deletions g4f/gui/server/backend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import g4f

from flask import request
from .internet import search
from .config import special_instructions
from .internet import get_search_message

g4f.debug.logging = True

Expand Down Expand Up @@ -53,10 +52,12 @@ def _gen_title(self):

def _conversation(self):
try:
#jailbreak = request.json['jailbreak']
#internet_access = request.json['meta']['content']['internet_access']
#conversation = request.json['meta']['content']['conversation']
#jailbreak = request.json['jailbreak']
web_search = request.json['meta']['content']['internet_access']
messages = request.json['meta']['content']['parts']
if web_search:
messages[-1]["content"] = get_search_message(messages[-1]["content"])
print(messages[-1]["content"])
model = request.json.get('model')
model = model if model else g4f.models.default
provider = request.json.get('provider', 'Auto').replace('g4f.Provider.', '')
Expand Down
Loading

0 comments on commit bc856b0

Please sign in to comment.