Skip to content

Commit

Permalink
Merge pull request #692 from mraniki/dev
Browse files Browse the repository at this point in the history
🥚 test crawlai
  • Loading branch information
mraniki authored Oct 27, 2024
2 parents 24acd55 + 681f036 commit 327f55b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .registry/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12
FROM python:3.13
WORKDIR /app
COPY ./examples/ ./.requirements/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
Expand Down
1 change: 1 addition & 0 deletions myllm/handler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# from myllm.handler.crawl4ai import Crawl4aiHandler
from myllm.handler.g4f import G4fHandler
from myllm.handler.groq import GroqHandler
from myllm.handler.openai import OpenaiHandler
Expand Down
70 changes: 0 additions & 70 deletions myllm/handler/archive/gemini.py

This file was deleted.

69 changes: 0 additions & 69 deletions myllm/handler/archive/petals.py

This file was deleted.

85 changes: 85 additions & 0 deletions myllm/handler/crawl4ai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# """
# 🔗 CrawlAI Support

# via https://github.com/unclecode/crawl4ai

# """

# from time import sleep

# from crawl4ai import AsyncWebCrawler
# from crawl4ai.extraction_strategy import LLMExtractionStrategy
# from loguru import logger
# from openai import OpenAI

# from .client import AIClient


# class Crawl4aiHandler(AIClient):
# """
# MyLLM class for Crawl4AI

# """

# def __init__(self, **kwargs):
# """
# Initialize the object with the given keyword arguments.

# :param kwargs: keyword arguments
# :return: None
# """

# super().__init__(**kwargs)
# if self.enabled and self.llm_provider_key:
# self.llm_base_url = kwargs.get("llm_base_url", None)
# self.client = OpenAI(
# api_key=self.llm_provider_key, base_url=self.llm_base_url
# )

# async def chat(self, prompt):
# """
# Asynchronously chats with the client based on the given prompt.

# :param prompt: The prompt for the chat.
# :return: The response from the chat.

# """

# self.conversation.add_message("user", prompt)
# archived_messages = self.conversation.get_messages()

# response = self.client.chat.completions.create(
# model=self.llm_model,
# messages=archived_messages,
# )
# sleep(self.timeout)

# if response_content := response.choices[0].message.content:
# self.conversation.add_message("assistant", response_content)
# return f"{self.llm_prefix} {response_content}"

# async def vision(self, prompt=None):
# """
# Asynchronously chats with the client based on the given prompt.

# :param prompt: The prompt for the chat.
# :return: The response from the chat.

# """

# async with AsyncWebCrawler(verbose=True) as crawler:
# result = await crawler.arun(
# url=self.browse_url,
# word_count_threshold=1,
# extraction_strategy=LLMExtractionStrategy(
# provider="openai/gpt-4o",
# api_token=self.llm_provider_key,
# # schema=None,
# # extraction_type="schema",
# instruction=self.vision_prompt,
# ),
# bypass_cache=True,
# )
# logger.debug("result {}", result)
# return result.extracted_content

34 changes: 22 additions & 12 deletions myllm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async def chat(self, prompt):
if _chats:
return "\n".join(_chats)

async def vision(self, base64_image):
async def vision(self, prompt=None):
"""
Asynchronously processes a base64-encoded image
and returns the responses from each client.
Expand All @@ -219,18 +219,28 @@ async def vision(self, base64_image):
from each client, separated by newlines.
"""
_chats = [
(
data
if len(self.clients) == 1 and not self.ai_agent_mode
else (
f"{self.ai_agent_prefix} {client.name}\n"
f"{data} {self.ai_agent_suffix}"
_chats = []
for client in self.clients:
try:
if prompt is None:
data = await client.vision()
else:
data = await client.vision(prompt=prompt)
if data is not None and data.strip():
if len(self.clients) == 1 and not self.ai_agent_mode:
_chats.append(data)
else:
_chats.append(
f"{self.ai_agent_prefix} {client.name}\n"
f"{data} {self.ai_agent_suffix}"
)
except AttributeError as e:
logger.error(
f"Error processing with {client.name}: {e}. "
"Make sure the client has a vision() method."
)
)
for client in self.clients
if (data := await client.vision(base64_image)) is not None and data.strip()
]
except Exception as e:
logger.error(f"Error processing with {client.name}: {e}")

if _chats:
return "\n".join(_chats)
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ js2py = "^0.74"
PyExecJS2="1.6.1"
curl_cffi = "0.7.2"
Brotli = "1.1.0"
openai = "1.51.0"
openai = "1.52.2"
groq = "0.11.0"
crawl4ai = "0.3.5"
playwright = "1.47.0"



[tool.poetry.group.dev.dependencies]
python-semantic-release = ">=8.0.8"
ruff = "^0.6.0"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ async def test_get_chats(talky):
assert found_keywords


# @pytest.mark.asyncio
# async def test_get_vision(talky):
# result = await talky.vision("tell me a story")
# assert result is not None
# found_keywords = [
# keyword for keyword in ["llama", "openai", "groq"] if keyword in result
# ]
# assert found_keywords


@pytest.mark.asyncio
async def test_export_chat_history(talky):
await talky.export_chat_history()
Expand Down

0 comments on commit 327f55b

Please sign in to comment.