-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# from myllm.handler.crawl4ai import Crawl4aiHandler | ||
from myllm.handler.g4f import G4fHandler | ||
from myllm.handler.groq import GroqHandler | ||
from myllm.handler.openai import OpenaiHandler | ||
from myllm.handler.crawl4ai import Crawl4aiHandler | ||
|
||
__all__ = ["Crawl4aiHandler","G4fHandler", "GroqHandler", "OpenaiHandler"] | ||
__all__ = ["G4fHandler", "GroqHandler", "OpenaiHandler"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,85 @@ | ||
""" | ||
🔗 CrawlAI Support | ||
# """ | ||
# 🔗 CrawlAI Support | ||
|
||
via https://github.com/unclecode/crawl4ai | ||
# via https://github.com/unclecode/crawl4ai | ||
|
||
""" | ||
# """ | ||
|
||
from time import sleep | ||
# from time import sleep | ||
|
||
from crawl4ai import AsyncWebCrawler | ||
from crawl4ai.extraction_strategy import LLMExtractionStrategy | ||
from loguru import logger | ||
from openai import OpenAI | ||
# from crawl4ai import AsyncWebCrawler | ||
# from crawl4ai.extraction_strategy import LLMExtractionStrategy | ||
# from loguru import logger | ||
# from openai import OpenAI | ||
|
||
from .client import AIClient | ||
# from .client import AIClient | ||
|
||
|
||
class Crawl4aiHandler(AIClient): | ||
""" | ||
MyLLM class for Crawl4AI | ||
# class Crawl4aiHandler(AIClient): | ||
# """ | ||
# MyLLM class for Crawl4AI | ||
|
||
""" | ||
# """ | ||
|
||
def __init__(self, **kwargs): | ||
""" | ||
Initialize the object with the given keyword arguments. | ||
# def __init__(self, **kwargs): | ||
# """ | ||
# Initialize the object with the given keyword arguments. | ||
|
||
:param kwargs: keyword arguments | ||
:return: None | ||
""" | ||
# :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 | ||
) | ||
# 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. | ||
# 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. | ||
# :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() | ||
# 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) | ||
# 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}" | ||
# 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. | ||
# 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. | ||
# :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 | ||
# 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 | ||
|