from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def init(self, model_index=0):
self.chatbot = None # Define the chatbot attribute first
code
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
Example usage
if name == "main":
llm = ConcreteHuggingChatLLM()
code
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method
# Stream the response
#for chunk in llm.stream("Tell me a joke"):
# print(chunk, end="", flush=True)
# Get available models
#print(llm.get_available_models())
# Switch to a different model
#llm.switch_model(1)
# Create a new conversation
#llm.new_conversation()
# Get conversation info
#info = llm.get_conversation_info()
#print(info.id, info.title, info.model, info.system_prompt, info.history)
# Search for an assistant
#assistant = llm.search_assistant("ChatGPT")
#llm.new_conversation(assistant=assistant, switch_to=True)
# Get assistant list
#assistant_list = llm.get_assistant_list()
#print(assistant_list)
# Delete all conversations (DANGER!)
#llm.delete_all_conversations()
The provided code is a Python class implementation of a ConcreteHuggingChatLLM, which is a subclass of the LLM class. This class integrates the Hugging Chat API to interact with a Large Language Model (LLM). Let's go through the code step by step:
Import Statements:
from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_env
import os
The necessary modules are imported, including typing for type annotations, LLM from langchain_core, hugchat and its Login class for interacting with the Hugging Chat API, and dotenv for loading environment variables.
ConcreteHuggingChatLLM Class Definition:
class ConcreteHuggingChatLLM(LLM):
def init(self, model_index=0):
self.chatbot = None # Define the chatbot attribute first
The class ConcreteHuggingChatLLM inherits from the LLM class. It has an init method that initializes the chatbot attribute.
Load Environment Variables:
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
The code loads environment variables from a .env file using the load_dotenv() function. It then retrieves the HF_EMAIL and HF_PASSWORD environment variables. If either of them is not set, it raises a ValueError.
Login to Hugging Chat API:
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
The code creates a Login instance with the provided email and password. It then calls the login method to authenticate with the Hugging Chat API and obtain cookies for subsequent requests.
Initialize Chatbot:
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
The chatbot attribute is initialized with an instance of the ChatBot class from the hugchat module. The cookies obtained from the login step are passed to the ChatBot constructor. The switch_llm method is called to select the desired LLM model based on the provided model_index.
_call Method:
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
The _call method is overridden from the LLM class. It takes a prompt string as input and uses the chatbot instance to send the prompt to the LLM. The response from the LLM is then returned as a string.
_identifying_params and _llm_type Properties:
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
These properties are used to provide identifying information about the LLM instance. _identifying_params returns an empty dictionary, indicating that there are no additional parameters that affect the behavior of the LLM. _llm_type returns the string "hugging_chat", indicating the type of LLM being used.
Example Usage:
if name == "main":
llm = ConcreteHuggingChatLLM()
code
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method
The example usage section demonstrates how to create an instance of ConcreteHuggingChatLLM and use it to generate a response to the prompt "Hello, how are you?". The generated response is then printed to the console.
Overall, this code provides a concrete implementation of an LLM using the Hugging Chat API, allowing users to interact with a Large Language Model and obtain responses to prompts.
i still got this error "Traceback (most recent call last):
File "C:\Users\AYOUB\Desktop\HugChat-huggignface_api\hugchat_api.txt", line 42, in
llm = ConcreteHuggingChatLLM()
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AYOUB\Desktop\HugChat-huggignface_api\hugchat_api.txt", line 10, in init
self.chatbot = None # Define the chatbot attribute first
^^^^^^^^^^^^
File "C:\Users\AYOUB\Desktop\HugChat-huggignface_api\myenv\Lib\site-packages\pydantic\v1\main.py", line 357, in setattr
raise ValueError(f'"{self.class.name}" object has no field "{name}"')
ValueError: "ConcreteHuggingChatLLM" object has no field "chatbot""
here is the detailed context that contains all the content of the python scritps imported in the previous script , here is the context "context :{{{
"here is the content of the file from langchain_core.language_models.llms import LLM":
"""Base LLM abstract class.
code
The purpose of this class is to expose a simpler interface for working
with LLMs, rather than expect the user to implement the full _generate method.
"""
@abstractmethod
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
return await run_in_executor(
None,
self._call,
prompt,
stop,
run_manager.get_sync() if run_manager else None,
**kwargs,
)
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
# TODO: add caching here.
generations = []
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
for prompt in prompts:
text = (
self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else self._call(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
async def _agenerate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
generations = []
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
for prompt in prompts:
text = (
await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else await self._acall(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)}"
for the file "from hugchat.login import Login" , here is its content {import requests
import os
import json
import logging
import re
class Login:
code
}" and for the file from hugchat import hugchat here is its content:"
"""Base LLM abstract class.
code
The purpose of this class is to expose a simpler interface for working
with LLMs, rather than expect the user to implement the full _generate method.
"""
@abstractmethod
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
return await run_in_executor(
None,
self._call,
prompt,
stop,
run_manager.get_sync() if run_manager else None,
**kwargs,
)
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
# TODO: add caching here.
generations = []
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
for prompt in prompts:
text = (
self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else self._call(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
async def _agenerate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
generations = []
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
for prompt in prompts:
text = (
await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else await self._acall(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
""
and for these files in hugchat 'from .message import Message
from . import exceptions' , here is there content "from typing import Generator, Union
from .exceptions import ChatError, ModelOverloadedError
import json
if name == "main":
pass
"and "class ModelOverloadedError(Exception):
"""
HF Model Overloaded Error
code
Raised when hf return response `{"error":"Model is overloaded", "error_type":"overloaded"}`
"""
pass
class ChatBotInitError(Exception):
"""
ChatBot Init Error
code
Raised when chatbot init failed
"""
pass
class CreateConversationError(Exception):
"""
Create Conversation Error
code
Raised when create conversation failed
"""
pass
class InvalidConversationIDError(Exception):
"""
Invalid Conversation ID Error
code
Raised when using a invalid conversation id
"""
pass
class DeleteConversationError(Exception):
"""
Delete Conversation Error
code
Raised when delete conversation failed
"""
pass
class ChatError(Exception):
"""
Chat Error
code
Raised when chat failed
"""
pass
"
and for login file in the hugchat library , here is its content "import requests
import os
import json
import logging
import re
class Login:
code
"}}}"
Assistant
The error you're encountering is rooted in a misunderstanding of the Python pydantic library's behavior when using it within your classes, which is indicated by the traceback pointing to pydantic. Your ConcreteHuggingChatLLM class inherits from LLM (which presumably inherits from a pydantic model, given the nature of the error), and you're attempting to dynamically add attributes (self.chatbot, in this case) that were not defined in the Pydantic model schema.
Pydantic models are primarily used for data validation and settings management, and they enforce schema constraints quite strictly. When you instantiate a Pydantic model, you can only assign values to fields that are explicitly declared in the model's definition. Adding new attributes dynamically at runtime, as you're trying to do with self.chatbot = None in the __init__ method, is not allowed and results in a ValueError.
Solution Approach:
There are a few ways to resolve this issue, depending on your specific architecture and requirements:
Modify Base Class: The most straightforward approach is to ensure that the base class (LLM) allows for dynamic attributes. This might not be immediately feasible if LLM must inherit from Pydantic's BaseModel and you don't control its implementation.
Use a Field with a Default Value: If you're able to modify the LLM class, consider declaring chatbot as an optional field of the appropriate type within the LLM model. For instance:
python
from pydantic import BaseModel, Field
from typing import Optional
class LLM(BaseModel):
chatbot: Optional[ChatBotType] = Field(default=None)
Replace ChatBotType with the actual type you're expecting for chatbot. If chatbot's type cannot be determined ahead of time or varies, you might use typing.Any.
Bypass Pydantic Validation for Dynamic Attributes: If you cannot modify the LLM class or its parents but still need to attach dynamic attributes, consider using __dict__ directly to bypass Pydantic's strict field validation:
python
self.__dict__['chatbot'] = None
User
each of your top three solutions should be detailed , here is the main scritp "from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def init(self, model_index=0):
self.chatbot = None # Define the chatbot attribute first
code
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
Example usage
if name == "main":
llm = ConcreteHuggingChatLLM()
code
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method
# Stream the response
#for chunk in llm.stream("Tell me a joke"):
# print(chunk, end="", flush=True)
# Get available models
#print(llm.get_available_models())
# Switch to a different model
#llm.switch_model(1)
# Create a new conversation
#llm.new_conversation()
# Get conversation info
#info = llm.get_conversation_info()
#print(info.id, info.title, info.model, info.system_prompt, info.history)
# Search for an assistant
#assistant = llm.search_assistant("ChatGPT")
#llm.new_conversation(assistant=assistant, switch_to=True)
# Get assistant list
#assistant_list = llm.get_assistant_list()
#print(assistant_list)
# Delete all conversations (DANGER!)
#llm.delete_all_conversations()
"
Assistant
Based on your script and the need to solve the issue of dynamically adding attributes to a subclass of potentially a Pydantic model or a similar framework enforcing schema constraints, let's delve into the three detailed solutions I previously outlined:
1. Modify Base Class to Include chatbot as a Field
If you have control over the LLM class (or can request the maintainers to consider a change), adding chatbot as an explicit field would be the most straightforward approach. This requires understanding the type of chatbot or using Python's typing module for a generic type if the type varies.
python
# In your LLM model definition
from pydantic import BaseModel
from typing import Optional, Any
class LLM(BaseModel):
chatbot: Optional[Any] = None # Use Any or a more specific type as needed
This would allow you to directly set self.chatbot in your subclass without bypassing Pydantic's validation system, as it now recognizes chatbot as a valid field.
2. Utilize __dict__ or setattr for Dynamic Attributes
If modifying the base class isn't an option, another approach is to directly manipulate the instance's attribute dictionary. This allows you to add attributes not defined in the Pydantic model but circumvents model validation, which might not be desirable in all cases.
Using __dict__:
python
self.__dict__['chatbot'] = None # Initialize in __init__
Using setattr:
python
setattr(self, 'chatbot', None) # Initialize in __init__
Both approaches achieve the same outcome but bypass Pydantic's strict schema enforcement. It's critical to ensure that doing this does not lead to inconsistencies in your application state or unexpected bugs, as it sidesteps the protections offered by Pydantic.
3. Architectural Revision: Consider Composition Over Inheritance
If both modifying the base class and bypassing validation are not plausible solutions, revisiting the design might reveal a more compatible approach. Composition over inheritance can offer more flexibility in cases like this.
Instead of inheriting from , consider having as a component of your class:
User
update this main script using the second option "from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def init(self, model_index=0):
self.chatbot = None # Define the chatbot attribute first
code
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
Example usage
if name == "main":
llm = ConcreteHuggingChatLLM()
code
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method
# Stream the response
#for chunk in llm.stream("Tell me a joke"):
# print(chunk, end="", flush=True)
# Get available models
#print(llm.get_available_models())
# Switch to a different model
#llm.switch_model(1)
# Create a new conversation
#llm.new_conversation()
# Get conversation info
#info = llm.get_conversation_info()
#print(info.id, info.title, info.model, info.system_prompt, info.history)
# Search for an assistant
#assistant = llm.search_assistant("ChatGPT")
#llm.new_conversation(assistant=assistant, switch_to=True)
# Get assistant list
#assistant_list = llm.get_assistant_list()
#print(assistant_list)
# Delete all conversations (DANGER!)
#llm.delete_all_conversations()
"
Assistant
Given the choice to apply the second solution, which involves bypassing Pydantic's validation for dynamic attributes by directly manipulating the instance's attribute dictionary or using setattr(), I'll update your ConcreteHuggingChatLLM class accordingly. This method allows us to add the chatbot attribute without it being explicitly declared in the LLM base class schema.
Here's how you can modify the initializer (__init__ method) of the ConcreteHuggingChatLLM class using setattr:
python
from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def __init__(self, model_index=0):
# Set 'chatbot' using setattr to bypass Pydantic's validation
setattr(self, 'chatbot', None) # Dynamically add the 'chatbot' attribute
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
# initialize chatbot after safely adding the attribute
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
# Example usage
if __name__ == "__main__":
llm = ConcreteHuggingChatLLM()
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method
# Add other example usages as needed
User
the script was run but i got not answer print
Assistant
Given that you've implemented the advice to utilize setattr for setting the chatbot attribute and the script executes without printing a response from the llm call to print(llm("Hello, how are you?")), let’s diagnose the potential causes and provide steps for troubleshooting the issue.
Ensure llm Callable Implementation:
In the original script provided, there's an invocation llm("Hello, how are you?"), which implies that the ConcreteHuggingChatLLM object is expected to be callable. However, the provided class does not implement the __call__ method, which is required to make an instance of the class callable.
Modify the class to implement the __call__ method, invoking _call within it, like so:
Check for Exceptions:
Ensure that the login and cookies retrieval processes are successful. If sign.login fails to return valid cookies, or if initialization of hugchat.ChatBot fails, there might not be an error message if exceptions are caught silently within those implementations. Verify by adding debugging prints or logs before and after these operations.
Validate hugchat.ChatBot Behavior:
Confirm that self.chatbot.chat(prompt) properly sends the request and receives a response. If there's an issue with network connectivity, API limitations, or incorrect usage of the API, the call might fail silently. To diagnose, you could directly test the hugchat.ChatBot functionality in isolation with hardcoded cookies to ensure it behaves as expected.
Inspect the Response:
It's possible that self.chatbot.chat(prompt) is executed successfully, but the response’s text attribute is empty or not as expected. Validate whether the response object has the expected structure and content by logging or printing the entire response object before accessing its text attribute.
Review Environment Variables and .env File:
Double-check that the .env file is correctly set up and located in a directory where the script has access, and that it contains valid HF_EMAIL and values. A simple misconfiguration or typo could prevent successful login.
User
update then this script "from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def init(self, model_index=0):
# Set 'chatbot' using setattr to bypass Pydantic's validation
setattr(self, 'chatbot', None) # Dynamically add the 'chatbot' attribute
code
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
# initialize chatbot after safely adding the attribute
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
Example usage
if name == "main":
llm = ConcreteHuggingChatLLM()
code
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Example of using the invoke method"
Assistant
To correct and update your script based on the guidance provided, let's first ensure that your ConcreteHuggingChatLLM class includes an implementation for the __call__ method. This will allow objects of this class to be callable, addressing the primary issue discussed.
Also, it seems there was a small typo in the __init__ method declaration (def init(self, model_index=0): should be def __init__(self, model_index=0):). And in the "main" code block, the conditional if __name__ == "main": should correctly be if __name__ == "__main__":.
Here's the corrected and updated version of your script:
python
from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def __init__(self, model_index=0): # Correct the method name here
# Set 'chatbot' using setattr to bypass Pydantic's validation
setattr(self, 'chatbot', None) # Dynamically add the 'chatbot' attribute
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
# initialize chatbot after safely adding the attribute
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
# Implement the callable functionality here by invoking _call
return self._call(prompt, stop)
@property
def _identifying_params(self):
"""Utility property for forcing cache keys to be unique"""
return {}
@property
def _llm_type(self):
return "hugging_chat"
# Example usage
if __name__ == "__main__": # Correct this line
llm = ConcreteHuggingChatLLM()
# Use the ConcreteHuggingChatLLM
print(llm("Hello, how are you?")) # Executing the call method
User
context :{{{
"here is the content of the file from langchain_core.language_models.llms import LLM":
"""Base LLM abstract class.
code
The purpose of this class is to expose a simpler interface for working
with LLMs, rather than expect the user to implement the full _generate method.
"""
@abstractmethod
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
return await run_in_executor(
None,
self._call,
prompt,
stop,
run_manager.get_sync() if run_manager else None,
**kwargs,
)
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
# TODO: add caching here.
generations = []
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
for prompt in prompts:
text = (
self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else self._call(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
async def _agenerate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
generations = []
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
for prompt in prompts:
text = (
await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else await self._acall(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)}"
Assistant
Given the detailed context you've provided, and focusing solely on the main script's incorrect behavior of not printing a response, it seems the absence of any output could stem from the lack of a direct invocation mechanism or issues in communication with the Hugging Chat API through your ConcreteHuggingChatLLM setup. Although specific debug logs or error messages are not provided here, I'll guide you through updating the script to ensure it functions as expected, assuming the rest of the architecture and integration (based on your context about LLM, Login, and chat processing) is set up correctly.
Here's how you can adjust the main script:
Add Call-Handling in the Custom LLM Class: Ensure your ConcreteHuggingChatLLM class can directly handle invocation calls, as recommended earlier.
Verify Main Block Activation: Ensure the execution block is correctly conditionalized under if __name__ == "__main__": to prevent any mishaps or misunderstandings there.
Integrate Debugging Logs: Add some print statements that could help in determining how far the script is executing without issues.
Here is the updated version of your main script:
python
User
here is the detailed context of all the files imported in the main file :
1-file 1: from langchain.language_models.llms import LLM
"
class LLM(BaseLLM):
"""Base LLM abstract class.
code
The purpose of this class is to expose a simpler interface for working
with LLMs, rather than expect the user to implement the full _generate method.
"""
@abstractmethod
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
return await run_in_executor(
None,
self._call,
prompt,
stop,
run_manager.get_sync() if run_manager else None,
**kwargs,
)
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
# TODO: add caching here.
generations = []
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
for prompt in prompts:
text = (
self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else self._call(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
async def _agenerate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
generations = []
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
for prompt in prompts:
text = (
await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else await self._acall(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
Assistant
Given the detailed context of the files imported in the main script, it appears you have a comprehensive setup for a chatbot application using Hugging Chat API along with a fundamental LLM (Large Language Model) interface from langchain. Based on the detailed context provided for each file, here's an integration-centric overview tailored for creating a succinct yet functional chatbot application.
Core Integration Points:
LLM Abstract Class Integration:
The LLM class from langchain establishes a protocol (interface) for working with language models.
Implementations should override _call method to interact with actual language model logic.
ChatBot Class Usage:
The ChatBot class encapsulates authentication, conversation management, and model interaction specific to Hugging Chat API.
It initializes sessions, manages cookies for authentication, and handles conversation state, besides sending and receiving messages.
Exception Handling:
Exception classes define a clear protocol for error handling across different stages of the chatbot's lifecycle, including initialization errors, conversation creation, and message processing.
Concrete Implementation:
To tie everything together, your concrete chatbot class should inherit from the LLM abstract class and utilize the ChatBot class internally for actual interactions with the Hugging Chat API.
Override the _call method to send prompts to the ChatBot instance and return responses.
Example Implementation:
Given the requirement and the context described, below is a high-level implementation sketched:
def __init__(self, email: str, passwd: str = "") -> None:
self.DEFAULT_PATH_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "usercookies")
self.DEFAULT_COOKIE_PATH = self.DEFAULT_PATH_DIR + os.path.join(f"{email}.json")
self.email: str = email
self.passwd: str = passwd
self.headers = {
"Referer": "https://huggingface.co/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64",
}
self.cookies = requests.sessions.RequestsCookieJar()
def login(self, cookie_dir_path: str = None, save_cookies: bool = False) -> requests.sessions.RequestsCookieJar:
'''
Login to huggingface.co with given email and password.
- If cookie_dir_path is given, load cookies from the path. If path is not exist, raise an exception.
- If save_cookies is True, save cookies to the givev path (if cookie_dir_path is not given, save to default path `./usercookies`).
- Return cookies if login success, otherwise raise an exception.
'''
if not cookie_dir_path:
cookie_dir_path = self.DEFAULT_PATH_DIR
# validate cookies content before use
if os.path.exists(cookie_dir_path) and os.path.exists(self._get_cookie_path(cookie_dir_path)):
with open(self._get_cookie_path(cookie_dir_path),'r+') as f_cookies:
cookies = json.load(f_cookies)
try:
list(cookies.keys()).index('token')
list(cookies.keys()).index('hf-chat')
return self.load_cookies(cookie_dir_path)
except Exception as e:
print('error during validating cookies')
self._sign_in_with_email()
location = self._get_auth_url()
if self._grant_auth(location):
if save_cookies:
self.save_cookies(cookie_dir_path)
return self.cookies
else:
raise Exception(f"Grant auth fatal, please check your email or password\ncookies gained: \n{self.cookies}")
def save_cookies(self, cookie_dir_path: str = './usercookies') -> str:
'''
cookies will be saved into: cookie_dir_path/<email>.json
'''
return self.saveCookiesToDir(cookie_dir_path)
def saveCookiesToDir(self, cookie_dir_path: str = './usercookies') -> str:
"""
alias of save_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
cookie_path = cookie_dir_path + f"{self.email}.json"
if not os.path.exists(cookie_dir_path):
logging.info("Cookie directory not exist, creating...")
os.makedirs(cookie_dir_path)
logging.info(f"Cookie store path: {cookie_path}")
with open(cookie_path, "w", encoding="utf-8") as f:
f.write(json.dumps(self.cookies.get_dict()))
return cookie_path
def load_cookies(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
'''
cookies will be loaded from: cookie_dir_path/<email>.json
'''
return self.loadCookiesFromDir(cookie_dir_path)
def loadCookiesFromDir(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
"""
alias of load_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
cookie_path = self._get_cookie_path(cookie_dir_path)
if not cookie_path:
raise Exception(f"Cookie not found. please check the path given: {cookie_dir_path}.\n" +
f"Cookie file must be named like this: 'your_email'+'.json': '{self.email}.json'")
with open(cookie_path, "r", encoding="utf-8") as f:
try:
js = json.loads(f.read())
for i in js.keys():
self.cookies.set(i, js[i])
logging.info(f"{i} loaded")
return self.cookies
except:
raise Exception("load cookies from files fatal. Please check the format")
def _request_get(self, url: str, params=None, allow_redirects=True) -> requests.Response:
res = requests.get(
url,
params=params,
headers=self.headers,
cookies=self.cookies,
allow_redirects=allow_redirects,
)
self._refresh_cookies(res.cookies)
return res
def _request_post(self, url: str, headers=None, params=None, data=None, stream=False,
allow_redirects=True) -> requests.Response:
res = requests.post(
url,
stream=stream,
params=params,
data=data,
headers=self.headers if headers is None else headers,
cookies=self.cookies,
allow_redirects=allow_redirects
)
self._refresh_cookies(res.cookies)
return res
def _refresh_cookies(self, cookies: requests.sessions.RequestsCookieJar):
dic = cookies.get_dict()
for i in dic:
self.cookies.set(i, dic[i])
def _sign_in_with_email(self):
"""
Login through your email and password.
PS: I found that it doesn't have any type of encryption till now,
which could expose your password to the internet.
"""
url = "https://huggingface.co/login"
data = {
"username": self.email,
"password": self.passwd,
}
res = self._request_post(url=url, data=data, allow_redirects=False)
if res.status_code == 400:
raise Exception("wrong username or password")
def _get_auth_url(self):
url = "https://huggingface.co/chat/login"
headers = {
"Referer": "https://huggingface.co/chat/login",
"User-Agent": self.headers["User-Agent"],
"Content-Type": "application/x-www-form-urlencoded"
}
res = self._request_post(url, headers=headers, allow_redirects=False)
if res.status_code == 200:
# location = res.headers.get("Location", None)
location = res.json()["location"]
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
elif res.status_code == 303:
location = res.headers.get("Location")
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
else:
raise Exception("Something went wrong!")
def _grant_auth(self, url: str) -> int:
res = self._request_get(url, allow_redirects=False)
if res.headers.__contains__("location"):
location = res.headers["location"]
res = self._request_get(location, allow_redirects=False)
if res.cookies.__contains__("hf-chat"):
return 1
# raise Exception("grantAuth fatal")
if res.status_code != 200:
raise Exception("grant auth fatal!")
csrf = re.findall('/oauth/authorize.*?name="csrf" value="(.*?)"', res.text)
if len(csrf) == 0:
raise Exception("No csrf found!")
data = {
"csrf": csrf[0]
}
res = self._request_post(url, data=data, allow_redirects=False)
if res.status_code != 303:
raise Exception(f"get hf-chat cookies fatal! - {res.status_code}")
else:
location = res.headers.get("Location")
res = self._request_get(location, allow_redirects=False)
if res.status_code != 302:
raise Exception(f"get hf-chat cookie fatal! - {res.status_code}")
else:
return 1
def _get_cookie_path(self, cookie_dir_path) -> str:
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
if not os.path.exists(cookie_dir_path):
return ""
files = os.listdir(cookie_dir_path)
for i in files:
if i == f"{self.email}.json":
return cookie_dir_path + i
return ""
A wrapper of `Generator` that receives and process the response
:Example:
.. code-block:: python
msg = bot.chat(...)
# stream process
for res in msg:
... # process
else:
if msg.done() == MSGSTATUS_REJECTED:
raise msg.error
# or simply use:
final = msg.wait_until_done()
"""
g: Generator
_stream_yield_all: bool = False
web_search: bool = False
web_search_sources: list = []
text: str = "" # For backward compatibility, we have to reserve the `text` field.
web_search_done: bool = not web_search
msg_status: int = MSGSTATUS_PENDING
error: Union[Exception, None] = None
def __init__(
self,
g: Generator,
_stream_yield_all: bool = False,
web_search: bool = False,
) -> None:
self.g = g
self._stream_yield_all = _stream_yield_all
self.web_search = web_search
def _filterResponse(self, obj: dict):
if not obj.__contains__("type"):
if obj.__contains__("message"):
raise ChatError(f"Server returns an error: {obj['message']}")
else:
raise ChatError(f"No `type` and `message` returned: {obj}")
def __next__(self) -> dict:
if self.msg_status == MSGSTATUS_RESOLVED:
raise StopIteration
elif self.msg_status == MSGSTATUS_REJECTED:
if self.error is not None:
raise self.error
else:
raise Exception(
"Message stauts is `Rejected` but no error found")
try:
a: dict = next(self.g)
self._filterResponse(a)
t: str = a["type"]
message_type: str = ""
if t == RESPONSE_TYPE_FINAL:
self.text = a["text"]
self.msg_status = MSGSTATUS_RESOLVED
elif t == RESPONSE_TYPE_WEB:
# gracefully pass unparseable webpages
if message_type != MSGTYPE_ERROR and a.__contains__("sources"):
self.web_search_sources.clear()
sources = a["sources"]
for source in sources:
wss = WebSearchSource()
wss.title = source["title"]
wss.link = source["link"]
wss.hostname = source["hostname"]
self.web_search_sources.append(wss)
elif "messageType" in a:
message_type: str = a["messageType"]
if message_type == MSGTYPE_ERROR:
self.error = ChatError(a["message"])
self.msg_status = MSGSTATUS_REJECTED
if t == RESPONSE_TYPE_STREAM:
self.web_search_done = True
elif t == RESPONSE_TYPE_STATUS:
pass
else:
if "Model is overloaded" in str(a):
self.error = ModelOverloadedError(
"Model is overloaded, please try again later or switch to another model."
)
self.msg_status = MSGSTATUS_REJECTED
elif a.__contains__(MSGTYPE_ERROR):
self.error = ChatError(a[MSGTYPE_ERROR])
self.msg_status = MSGSTATUS_REJECTED
else:
self.error = ChatError(f"Unknown json response: {a}")
# If _stream_yield_all is True, yield all responses from the server.
if self._stream_yield_all or t == RESPONSE_TYPE_STREAM:
return a
else:
return self.__next__()
except StopIteration:
if self.msg_status == MSGSTATUS_PENDING:
self.error = ChatError(
"Stream of responses has abruptly ended (final answer has not been received)."
)
raise self.error
pass
except Exception as e:
# print("meet error: ", str(e))
self.error = e
self.msg_status = MSGSTATUS_REJECTED
raise self.error
def __iter__(self):
return self
def throw(
self,
__typ,
__val=None,
__tb=None,
):
return self.g.throw(__typ, __val, __tb)
def send(self, __value):
return self.g.send(__value)
def get_final_text(self) -> str:
"""
:Return:
- self.text
"""
return self.text
def get_search_sources(self) -> list:
"""
:Return:
- self.web_search_sources
"""
return self.web_search_sources
def search_enabled(self) -> bool:
"""
:Return:
- self.web_search
"""
return self.web_search
def wait_until_done(self) -> str:
"""
:Return:
- self.text if resolved else raise error
wait until every response is resolved
"""
while not self.is_done():
self.__next__()
if self.is_done() == MSGSTATUS_RESOLVED:
return self.text
elif self.error is not None:
raise self.error
else:
raise Exception("Rejected but no error captured!")
def is_done(self):
"""
:Return:
- self.msg_status
3 status:
- MSGSTATUS_PENDING = 0 # running
- MSGSTATUS_RESOLVED = 1 # done with no error(maybe?)
- MSGSTATUS_REJECTED = 2 # error raised
"""
return self.msg_status
def is_done_search(self):
"""
:Return:
- self.web_search_done
web search result will be set to `done` once the token is received
"""
return self.web_search_done
def __str__(self):
return self.wait_until_done()
def __getitem__(self, key: str) -> str:
print("_getitem_")
self.wait_until_done()
print("done")
if key == "text":
return self.text
elif key == "web_search":
return self.web_search
elif key == "web_search_sources":
return self.web_search_sources
def __add__(self, other: str) -> str:
self.wait_until_done()
return self.text + other
def __radd__(self, other: str) -> str:
self.wait_until_done()
return other + self.text
def __iadd__(self, other: str) -> str:
self.wait_until_done()
self.text += other
return self.text
def __init__(self, email: str, passwd: str = "") -> None:
self.DEFAULT_PATH_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "usercookies")
self.DEFAULT_COOKIE_PATH = self.DEFAULT_PATH_DIR + os.path.join(f"{email}.json")
self.email: str = email
self.passwd: str = passwd
self.headers = {
"Referer": "https://huggingface.co/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64",
}
self.cookies = requests.sessions.RequestsCookieJar()
def login(self, cookie_dir_path: str = None, save_cookies: bool = False) -> requests.sessions.RequestsCookieJar:
'''
Login to huggingface.co with given email and password.
- If cookie_dir_path is given, load cookies from the path. If path is not exist, raise an exception.
- If save_cookies is True, save cookies to the givev path (if cookie_dir_path is not given, save to default path `./usercookies`).
- Return cookies if login success, otherwise raise an exception.
'''
if not cookie_dir_path:
cookie_dir_path = self.DEFAULT_PATH_DIR
# validate cookies content before use
if os.path.exists(cookie_dir_path) and os.path.exists(self._get_cookie_path(cookie_dir_path)):
with open(self._get_cookie_path(cookie_dir_path),'r+') as f_cookies:
cookies = json.load(f_cookies)
try:
list(cookies.keys()).index('token')
list(cookies.keys()).index('hf-chat')
return self.load_cookies(cookie_dir_path)
except Exception as e:
print('error during validating cookies')
self._sign_in_with_email()
location = self._get_auth_url()
if self._grant_auth(location):
if save_cookies:
self.save_cookies(cookie_dir_path)
return self.cookies
else:
raise Exception(f"Grant auth fatal, please check your email or password\ncookies gained: \n{self.cookies}")
def save_cookies(self, cookie_dir_path: str = './usercookies') -> str:
'''
cookies will be saved into: cookie_dir_path/<email>.json
'''
return self.saveCookiesToDir(cookie_dir_path)
def saveCookiesToDir(self, cookie_dir_path: str = './usercookies') -> str:
"""
alias of save_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
cookie_path = cookie_dir_path + f"{self.email}.json"
if not os.path.exists(cookie_dir_path):
logging.info("Cookie directory not exist, creating...")
os.makedirs(cookie_dir_path)
logging.info(f"Cookie store path: {cookie_path}")
with open(cookie_path, "w", encoding="utf-8") as f:
f.write(json.dumps(self.cookies.get_dict()))
return cookie_path
def load_cookies(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
'''
cookies will be loaded from: cookie_dir_path/<email>.json
'''
return self.loadCookiesFromDir(cookie_dir_path)
def loadCookiesFromDir(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
"""
alias of load_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
cookie_path = self._get_cookie_path(cookie_dir_path)
if not cookie_path:
raise Exception(f"Cookie not found. please check the path given: {cookie_dir_path}.\n" +
f"Cookie file must be named like this: 'your_email'+'.json': '{self.email}.json'")
with open(cookie_path, "r", encoding="utf-8") as f:
try:
js = json.loads(f.read())
for i in js.keys():
self.cookies.set(i, js[i])
logging.info(f"{i} loaded")
return self.cookies
except:
raise Exception("load cookies from files fatal. Please check the format")
def _request_get(self, url: str, params=None, allow_redirects=True) -> requests.Response:
res = requests.get(
url,
params=params,
headers=self.headers,
cookies=self.cookies,
allow_redirects=allow_redirects,
)
self._refresh_cookies(res.cookies)
return res
def _request_post(self, url: str, headers=None, params=None, data=None, stream=False,
allow_redirects=True) -> requests.Response:
res = requests.post(
url,
stream=stream,
params=params,
data=data,
headers=self.headers if headers is None else headers,
cookies=self.cookies,
allow_redirects=allow_redirects
)
self._refresh_cookies(res.cookies)
return res
def _refresh_cookies(self, cookies: requests.sessions.RequestsCookieJar):
dic = cookies.get_dict()
for i in dic:
self.cookies.set(i, dic[i])
def _sign_in_with_email(self):
"""
Login through your email and password.
PS: I found that it doesn't have any type of encryption till now,
which could expose your password to the internet.
"""
url = "https://huggingface.co/login"
data = {
"username": self.email,
"password": self.passwd,
}
res = self._request_post(url=url, data=data, allow_redirects=False)
if res.status_code == 400:
raise Exception("wrong username or password")
def _get_auth_url(self):
url = "https://huggingface.co/chat/login"
headers = {
"Referer": "https://huggingface.co/chat/login",
"User-Agent": self.headers["User-Agent"],
"Content-Type": "application/x-www-form-urlencoded"
}
res = self._request_post(url, headers=headers, allow_redirects=False)
if res.status_code == 200:
# location = res.headers.get("Location", None)
location = res.json()["location"]
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
elif res.status_code == 303:
location = res.headers.get("Location")
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
else:
raise Exception("Something went wrong!")
def _grant_auth(self, url: str) -> int:
res = self._request_get(url, allow_redirects=False)
if res.headers.__contains__("location"):
location = res.headers["location"]
res = self._request_get(location, allow_redirects=False)
if res.cookies.__contains__("hf-chat"):
return 1
# raise Exception("grantAuth fatal")
if res.status_code != 200:
raise Exception("grant auth fatal!")
csrf = re.findall('/oauth/authorize.*?name="csrf" value="(.*?)"', res.text)
if len(csrf) == 0:
raise Exception("No csrf found!")
data = {
"csrf": csrf[0]
}
res = self._request_post(url, data=data, allow_redirects=False)
if res.status_code != 303:
raise Exception(f"get hf-chat cookies fatal! - {res.status_code}")
else:
location = res.headers.get("Location")
res = self._request_get(location, allow_redirects=False)
if res.status_code != 302:
raise Exception(f"get hf-chat cookie fatal! - {res.status_code}")
else:
return 1
def _get_cookie_path(self, cookie_dir_path) -> str:
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
if not os.path.exists(cookie_dir_path):
return ""
files = os.listdir(cookie_dir_path)
for i in files:
if i == f"{self.email}.json":
return cookie_dir_path + i
return ""
or
python
setattr(self, 'chatbot', None)
This workaround directly manipulates the instance's attribute dictionary, bypassing Pydantic's setters. Be mindful that this approach circumvents the validation and error-checking benefits that Pydantic offers.
Review Architectural Needs: If none of the above solutions are practical, it might be worth revisiting the overall architecture to see if there's a way to redesign the interaction between your class and the underlying Pydantic model. For instance, consider composition over inheritance if adding dynamic attributes is a core requirement and doesn't fit well with Pydantic's model constraints.
Choose the approach that best matches your context and the constraints of the libraries you are working with.
LLM
LLM
ConcreteHuggingChatLLM
python
from typing import Optional, List
from some_module import LLM # Assuming you can't modify this directly
class ConcreteHuggingChatLLM:
def __init__(self, model_index=0):
self.llm: LLM = LLM(...) # Initialize the LLM component as needed
# Rest of your initialization code
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.llm.some_llm_specific_method(prompt) # Use the LLM component
return response
This way, you encapsulate the LLM functionality within your class without inheriting from it directly, giving you the latitude to introduce any additional attributes or methods necessary for your application. This approach often leads to more flexible and maintainable code, especially when interfacing with external libraries or frameworks where you have limited control over their internals.
Each of these solutions caters to different scenarios based on the flexibility of the existing codebase, performance, and future maintainability considerations. They offer varying degrees of integration depth with external libraries and frameworks, highlighting the importance of carefully choosing the one that aligns with your project's needs.
This modified version employs setattr to dynamically add the chatbot attribute to instances of ConcreteHuggingChatLLM. This approach is useful when you cannot or prefer not to alter the LLM base class schema to include the chatbot field directly. However, it's essential to be aware that this method bypasses some of the protections against incorrect data usage that are built into Pydantic, so you should use it judiciously and ensure you handle the attribute correctly elsewhere in your code.
HF_PASSWORD
Debugging Statements:
Insert print statements at critical points in the script (after setting self.chatbot, before and after self.chatbot.chat(prompt), etc.) to ensure that each step is executed as expected. Tracing the execution flow this way can help identify where the script's behavior diverges from expectations.
After implementing the __call__ method and ensuring all parts of the script operate correctly, run the script again. If it still does not produce the expected output, the issue might require a deeper investigation into the hugchat library's behavior, network issues, or potential API changes.
If the issue persists even after these troubleshooting steps, please provide more details about any error messages, the behavior of the hugchat.ChatBot calls in isolation, and any other context that could aid further diagnosis.
This script includes the necessary __call__ method implementation, enabling the ConcreteHuggingChatLLM objects to be invoked directly with a prompt string as an argument. Additionally, it corrects the method name to __init__ for proper class initialization and fixes the conditional check in the "main" block.
Running this script should now successfully initialize an instance of ConcreteHuggingChatLLM, log in to obtain cookies, set up a chatbot with the cookies, and then call the chatbot with a given prompt when you execute print(llm("Hello, how are you?")).
for the file "from hugchat.login import Login" , here is its content {import requests
import os
import json
import logging
import re
class Login:
code
def __init__(self, email: str, passwd: str = "") -> None:
self.DEFAULT_PATH_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "usercookies")
self.DEFAULT_COOKIE_PATH = self.DEFAULT_PATH_DIR + os.path.join(f"{email}.json")
self.email: str = email
self.passwd: str = passwd
self.headers = {
"Referer": "https://huggingface.co/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64",
}
self.cookies = requests.sessions.RequestsCookieJar()
def login(self, cookie_dir_path: str = None, save_cookies: bool = False) -> requests.sessions.RequestsCookieJar:
'''
Login to huggingface.co with given email and password.
- If cookie_dir_path is given, load cookies from the path. If path is not exist, raise an exception.
- If save_cookies is True, save cookies to the givev path (if cookie_dir_path is not given, save to default path `./usercookies`).
- Return cookies if login success, otherwise raise an exception.
'''
if not cookie_dir_path:
cookie_dir_path = self.DEFAULT_PATH_DIR
# validate cookies content before use
if os.path.exists(cookie_dir_path) and os.path.exists(self._get_cookie_path(cookie_dir_path)):
with open(self._get_cookie_path(cookie_dir_path),'r+') as f_cookies:
cookies = json.load(f_cookies)
try:
list(cookies.keys()).index('token')
list(cookies.keys()).index('hf-chat')
return self.load_cookies(cookie_dir_path)
except Exception as e:
print('error during validating cookies')
self._sign_in_with_email()
location = self._get_auth_url()
if self._grant_auth(location):
if save_cookies:
self.save_cookies(cookie_dir_path)
return self.cookies
else:
raise Exception(f"Grant auth fatal, please check your email or password\ncookies gained: \n{self.cookies}")
def save_cookies(self, cookie_dir_path: str = './usercookies') -> str:
'''
cookies will be saved into: cookie_dir_path/<email>.json
'''
return self.saveCookiesToDir(cookie_dir_path)
def saveCookiesToDir(self, cookie_dir_path: str = './usercookies') -> str:
"""
alias of save_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
cookie_path = cookie_dir_path + f"{self.email}.json"
if not os.path.exists(cookie_dir_path):
logging.info("Cookie directory not exist, creating...")
os.makedirs(cookie_dir_path)
logging.info(f"Cookie store path: {cookie_path}")
with open(cookie_path, "w", encoding="utf-8") as f:
f.write(json.dumps(self.cookies.get_dict()))
return cookie_path
def load_cookies(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
'''
cookies will be loaded from: cookie_dir_path/<email>.json
'''
return self.loadCookiesFromDir(cookie_dir_path)
def loadCookiesFromDir(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
"""
alias of load_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
cookie_path = self._get_cookie_path(cookie_dir_path)
if not cookie_path:
raise Exception(f"Cookie not found. please check the path given: {cookie_dir_path}.\n" +
f"Cookie file must be named like this: 'your_email'+'.json': '{self.email}.json'")
with open(cookie_path, "r", encoding="utf-8") as f:
try:
js = json.loads(f.read())
for i in js.keys():
self.cookies.set(i, js[i])
logging.info(f"{i} loaded")
return self.cookies
except:
raise Exception("load cookies from files fatal. Please check the format")
def _request_get(self, url: str, params=None, allow_redirects=True) -> requests.Response:
res = requests.get(
url,
params=params,
headers=self.headers,
cookies=self.cookies,
allow_redirects=allow_redirects,
)
self._refresh_cookies(res.cookies)
return res
def _request_post(self, url: str, headers=None, params=None, data=None, stream=False,
allow_redirects=True) -> requests.Response:
res = requests.post(
url,
stream=stream,
params=params,
data=data,
headers=self.headers if headers is None else headers,
cookies=self.cookies,
allow_redirects=allow_redirects
)
self._refresh_cookies(res.cookies)
return res
def _refresh_cookies(self, cookies: requests.sessions.RequestsCookieJar):
dic = cookies.get_dict()
for i in dic:
self.cookies.set(i, dic[i])
def _sign_in_with_email(self):
"""
Login through your email and password.
PS: I found that it doesn't have any type of encryption till now,
which could expose your password to the internet.
"""
url = "https://huggingface.co/login"
data = {
"username": self.email,
"password": self.passwd,
}
res = self._request_post(url=url, data=data, allow_redirects=False)
if res.status_code == 400:
raise Exception("wrong username or password")
def _get_auth_url(self):
url = "https://huggingface.co/chat/login"
headers = {
"Referer": "https://huggingface.co/chat/login",
"User-Agent": self.headers["User-Agent"],
"Content-Type": "application/x-www-form-urlencoded"
}
res = self._request_post(url, headers=headers, allow_redirects=False)
if res.status_code == 200:
# location = res.headers.get("Location", None)
location = res.json()["location"]
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
elif res.status_code == 303:
location = res.headers.get("Location")
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
else:
raise Exception("Something went wrong!")
def _grant_auth(self, url: str) -> int:
res = self._request_get(url, allow_redirects=False)
if res.headers.__contains__("location"):
location = res.headers["location"]
res = self._request_get(location, allow_redirects=False)
if res.cookies.__contains__("hf-chat"):
return 1
# raise Exception("grantAuth fatal")
if res.status_code != 200:
raise Exception("grant auth fatal!")
csrf = re.findall('/oauth/authorize.*?name="csrf" value="(.*?)"', res.text)
if len(csrf) == 0:
raise Exception("No csrf found!")
data = {
"csrf": csrf[0]
}
res = self._request_post(url, data=data, allow_redirects=False)
if res.status_code != 303:
raise Exception(f"get hf-chat cookies fatal! - {res.status_code}")
else:
location = res.headers.get("Location")
res = self._request_get(location, allow_redirects=False)
if res.status_code != 302:
raise Exception(f"get hf-chat cookie fatal! - {res.status_code}")
else:
return 1
def _get_cookie_path(self, cookie_dir_path) -> str:
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
if not os.path.exists(cookie_dir_path):
return ""
files = os.listdir(cookie_dir_path)
for i in files:
if i == f"{self.email}.json":
return cookie_dir_path + i
return ""
}" and for the file from hugchat import hugchat here is its content:"
"""Base LLM abstract class.
code
The purpose of this class is to expose a simpler interface for working
with LLMs, rather than expect the user to implement the full _generate method.
"""
@abstractmethod
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Run the LLM on the given prompt and input."""
return await run_in_executor(
None,
self._call,
prompt,
stop,
run_manager.get_sync() if run_manager else None,
**kwargs,
)
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
# TODO: add caching here.
generations = []
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
for prompt in prompts:
text = (
self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else self._call(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
async def _agenerate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
"""Run the LLM on the given prompt and input."""
generations = []
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
for prompt in prompts:
text = (
await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
if new_arg_supported
else await self._acall(prompt, stop=stop, **kwargs)
)
generations.append([Generation(text=text)])
return LLMResult(generations=generations)
""
and for these files in hugchat 'from .message import Message
from . import exceptions' , here is there content "from typing import Generator, Union
from .exceptions import ChatError, ModelOverloadedError
import json
A wrapper of `Generator` that receives and process the response
:Example:
.. code-block:: python
msg = bot.chat(...)
# stream process
for res in msg:
... # process
else:
if msg.done() == MSGSTATUS_REJECTED:
raise msg.error
# or simply use:
final = msg.wait_until_done()
"""
g: Generator
_stream_yield_all: bool = False
web_search: bool = False
web_search_sources: list = []
text: str = "" # For backward compatibility, we have to reserve the `text` field.
web_search_done: bool = not web_search
msg_status: int = MSGSTATUS_PENDING
error: Union[Exception, None] = None
def __init__(
self,
g: Generator,
_stream_yield_all: bool = False,
web_search: bool = False,
) -> None:
self.g = g
self._stream_yield_all = _stream_yield_all
self.web_search = web_search
def _filterResponse(self, obj: dict):
if not obj.__contains__("type"):
if obj.__contains__("message"):
raise ChatError(f"Server returns an error: {obj['message']}")
else:
raise ChatError(f"No `type` and `message` returned: {obj}")
def __next__(self) -> dict:
if self.msg_status == MSGSTATUS_RESOLVED:
raise StopIteration
elif self.msg_status == MSGSTATUS_REJECTED:
if self.error is not None:
raise self.error
else:
raise Exception(
"Message stauts is `Rejected` but no error found")
try:
a: dict = next(self.g)
self._filterResponse(a)
t: str = a["type"]
message_type: str = ""
if t == RESPONSE_TYPE_FINAL:
self.text = a["text"]
self.msg_status = MSGSTATUS_RESOLVED
elif t == RESPONSE_TYPE_WEB:
# gracefully pass unparseable webpages
if message_type != MSGTYPE_ERROR and a.__contains__("sources"):
self.web_search_sources.clear()
sources = a["sources"]
for source in sources:
wss = WebSearchSource()
wss.title = source["title"]
wss.link = source["link"]
wss.hostname = source["hostname"]
self.web_search_sources.append(wss)
elif "messageType" in a:
message_type: str = a["messageType"]
if message_type == MSGTYPE_ERROR:
self.error = ChatError(a["message"])
self.msg_status = MSGSTATUS_REJECTED
if t == RESPONSE_TYPE_STREAM:
self.web_search_done = True
elif t == RESPONSE_TYPE_STATUS:
pass
else:
if "Model is overloaded" in str(a):
self.error = ModelOverloadedError(
"Model is overloaded, please try again later or switch to another model."
)
self.msg_status = MSGSTATUS_REJECTED
elif a.__contains__(MSGTYPE_ERROR):
self.error = ChatError(a[MSGTYPE_ERROR])
self.msg_status = MSGSTATUS_REJECTED
else:
self.error = ChatError(f"Unknown json response: {a}")
# If _stream_yield_all is True, yield all responses from the server.
if self._stream_yield_all or t == RESPONSE_TYPE_STREAM:
return a
else:
return self.__next__()
except StopIteration:
if self.msg_status == MSGSTATUS_PENDING:
self.error = ChatError(
"Stream of responses has abruptly ended (final answer has not been received)."
)
raise self.error
pass
except Exception as e:
# print("meet error: ", str(e))
self.error = e
self.msg_status = MSGSTATUS_REJECTED
raise self.error
def __iter__(self):
return self
def throw(
self,
__typ,
__val=None,
__tb=None,
):
return self.g.throw(__typ, __val, __tb)
def send(self, __value):
return self.g.send(__value)
def get_final_text(self) -> str:
"""
:Return:
- self.text
"""
return self.text
def get_search_sources(self) -> list:
"""
:Return:
- self.web_search_sources
"""
return self.web_search_sources
def search_enabled(self) -> bool:
"""
:Return:
- self.web_search
"""
return self.web_search
def wait_until_done(self) -> str:
"""
:Return:
- self.text if resolved else raise error
wait until every response is resolved
"""
while not self.is_done():
self.__next__()
if self.is_done() == MSGSTATUS_RESOLVED:
return self.text
elif self.error is not None:
raise self.error
else:
raise Exception("Rejected but no error captured!")
def is_done(self):
"""
:Return:
- self.msg_status
3 status:
- MSGSTATUS_PENDING = 0 # running
- MSGSTATUS_RESOLVED = 1 # done with no error(maybe?)
- MSGSTATUS_REJECTED = 2 # error raised
"""
return self.msg_status
def is_done_search(self):
"""
:Return:
- self.web_search_done
web search result will be set to `done` once the token is received
"""
return self.web_search_done
def __str__(self):
return self.wait_until_done()
def __getitem__(self, key: str) -> str:
print("_getitem_")
self.wait_until_done()
print("done")
if key == "text":
return self.text
elif key == "web_search":
return self.web_search
elif key == "web_search_sources":
return self.web_search_sources
def __add__(self, other: str) -> str:
self.wait_until_done()
return self.text + other
def __radd__(self, other: str) -> str:
self.wait_until_done()
return other + self.text
def __iadd__(self, other: str) -> str:
self.wait_until_done()
self.text += other
return self.text
if name == "main":
pass
"and "class ModelOverloadedError(Exception):
"""
HF Model Overloaded Error
code
Raised when hf return response `{"error":"Model is overloaded", "error_type":"overloaded"}`
"""
pass
class ChatBotInitError(Exception):
"""
ChatBot Init Error
code
Raised when chatbot init failed
"""
pass
class CreateConversationError(Exception):
"""
Create Conversation Error
code
Raised when create conversation failed
"""
pass
class InvalidConversationIDError(Exception):
"""
Invalid Conversation ID Error
code
Raised when using a invalid conversation id
"""
pass
class DeleteConversationError(Exception):
"""
Delete Conversation Error
code
Raised when delete conversation failed
"""
pass
class ChatError(Exception):
"""
Chat Error
code
Raised when chat failed
"""
pass
"
and for login file in the hugchat library , here is its content "import requests
import os
import json
import logging
import re
class Login:
code
def __init__(self, email: str, passwd: str = "") -> None:
self.DEFAULT_PATH_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "usercookies")
self.DEFAULT_COOKIE_PATH = self.DEFAULT_PATH_DIR + os.path.join(f"{email}.json")
self.email: str = email
self.passwd: str = passwd
self.headers = {
"Referer": "https://huggingface.co/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64",
}
self.cookies = requests.sessions.RequestsCookieJar()
def login(self, cookie_dir_path: str = None, save_cookies: bool = False) -> requests.sessions.RequestsCookieJar:
'''
Login to huggingface.co with given email and password.
- If cookie_dir_path is given, load cookies from the path. If path is not exist, raise an exception.
- If save_cookies is True, save cookies to the givev path (if cookie_dir_path is not given, save to default path `./usercookies`).
- Return cookies if login success, otherwise raise an exception.
'''
if not cookie_dir_path:
cookie_dir_path = self.DEFAULT_PATH_DIR
# validate cookies content before use
if os.path.exists(cookie_dir_path) and os.path.exists(self._get_cookie_path(cookie_dir_path)):
with open(self._get_cookie_path(cookie_dir_path),'r+') as f_cookies:
cookies = json.load(f_cookies)
try:
list(cookies.keys()).index('token')
list(cookies.keys()).index('hf-chat')
return self.load_cookies(cookie_dir_path)
except Exception as e:
print('error during validating cookies')
self._sign_in_with_email()
location = self._get_auth_url()
if self._grant_auth(location):
if save_cookies:
self.save_cookies(cookie_dir_path)
return self.cookies
else:
raise Exception(f"Grant auth fatal, please check your email or password\ncookies gained: \n{self.cookies}")
def save_cookies(self, cookie_dir_path: str = './usercookies') -> str:
'''
cookies will be saved into: cookie_dir_path/<email>.json
'''
return self.saveCookiesToDir(cookie_dir_path)
def saveCookiesToDir(self, cookie_dir_path: str = './usercookies') -> str:
"""
alias of save_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
cookie_path = cookie_dir_path + f"{self.email}.json"
if not os.path.exists(cookie_dir_path):
logging.info("Cookie directory not exist, creating...")
os.makedirs(cookie_dir_path)
logging.info(f"Cookie store path: {cookie_path}")
with open(cookie_path, "w", encoding="utf-8") as f:
f.write(json.dumps(self.cookies.get_dict()))
return cookie_path
def load_cookies(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
'''
cookies will be loaded from: cookie_dir_path/<email>.json
'''
return self.loadCookiesFromDir(cookie_dir_path)
def loadCookiesFromDir(self, cookie_dir_path: str = './usercookies') -> requests.sessions.RequestsCookieJar:
"""
alias of load_cookies
"""
cookie_dir_path = self.DEFAULT_PATH_DIR if not cookie_dir_path else cookie_dir_path
cookie_path = self._get_cookie_path(cookie_dir_path)
if not cookie_path:
raise Exception(f"Cookie not found. please check the path given: {cookie_dir_path}.\n" +
f"Cookie file must be named like this: 'your_email'+'.json': '{self.email}.json'")
with open(cookie_path, "r", encoding="utf-8") as f:
try:
js = json.loads(f.read())
for i in js.keys():
self.cookies.set(i, js[i])
logging.info(f"{i} loaded")
return self.cookies
except:
raise Exception("load cookies from files fatal. Please check the format")
def _request_get(self, url: str, params=None, allow_redirects=True) -> requests.Response:
res = requests.get(
url,
params=params,
headers=self.headers,
cookies=self.cookies,
allow_redirects=allow_redirects,
)
self._refresh_cookies(res.cookies)
return res
def _request_post(self, url: str, headers=None, params=None, data=None, stream=False,
allow_redirects=True) -> requests.Response:
res = requests.post(
url,
stream=stream,
params=params,
data=data,
headers=self.headers if headers is None else headers,
cookies=self.cookies,
allow_redirects=allow_redirects
)
self._refresh_cookies(res.cookies)
return res
def _refresh_cookies(self, cookies: requests.sessions.RequestsCookieJar):
dic = cookies.get_dict()
for i in dic:
self.cookies.set(i, dic[i])
def _sign_in_with_email(self):
"""
Login through your email and password.
PS: I found that it doesn't have any type of encryption till now,
which could expose your password to the internet.
"""
url = "https://huggingface.co/login"
data = {
"username": self.email,
"password": self.passwd,
}
res = self._request_post(url=url, data=data, allow_redirects=False)
if res.status_code == 400:
raise Exception("wrong username or password")
def _get_auth_url(self):
url = "https://huggingface.co/chat/login"
headers = {
"Referer": "https://huggingface.co/chat/login",
"User-Agent": self.headers["User-Agent"],
"Content-Type": "application/x-www-form-urlencoded"
}
res = self._request_post(url, headers=headers, allow_redirects=False)
if res.status_code == 200:
# location = res.headers.get("Location", None)
location = res.json()["location"]
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
elif res.status_code == 303:
location = res.headers.get("Location")
if location:
return location
else:
raise Exception("No authorize url found, please check your email or password.")
else:
raise Exception("Something went wrong!")
def _grant_auth(self, url: str) -> int:
res = self._request_get(url, allow_redirects=False)
if res.headers.__contains__("location"):
location = res.headers["location"]
res = self._request_get(location, allow_redirects=False)
if res.cookies.__contains__("hf-chat"):
return 1
# raise Exception("grantAuth fatal")
if res.status_code != 200:
raise Exception("grant auth fatal!")
csrf = re.findall('/oauth/authorize.*?name="csrf" value="(.*?)"', res.text)
if len(csrf) == 0:
raise Exception("No csrf found!")
data = {
"csrf": csrf[0]
}
res = self._request_post(url, data=data, allow_redirects=False)
if res.status_code != 303:
raise Exception(f"get hf-chat cookies fatal! - {res.status_code}")
else:
location = res.headers.get("Location")
res = self._request_get(location, allow_redirects=False)
if res.status_code != 302:
raise Exception(f"get hf-chat cookie fatal! - {res.status_code}")
else:
return 1
def _get_cookie_path(self, cookie_dir_path) -> str:
if not cookie_dir_path.endswith("/"):
cookie_dir_path += "/"
if not os.path.exists(cookie_dir_path):
return ""
files = os.listdir(cookie_dir_path)
for i in files:
if i == f"{self.email}.json":
return cookie_dir_path + i
return ""
"}}}
from typing import Optional, List
from langchain_core.language_models.llms import LLM
from hugchat import hugchat
from hugchat.login import Login
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def __init__(self, model_index=0):
setattr(self, 'chatbot', None) # Dynamically add the 'chatbot' attribute
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
if not hf_email or not hf_password:
raise ValueError("HF_EMAIL and HF_PASSWORD must be set in the .env file")
print("Logging in with provided credentials...")
cookie_path_dir = "./cookies/"
sign = Login(hf_email, hf_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
print("Initializing chatbot with cookies...")
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.chatbot.chat(prompt)
return response.text
def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
return self._call(prompt, stop)
@property
def _identifying_params(self):
return {}
@property
def _llm_type(self):
return "hugging_chat"
if __name__ == "__main__":
print("Creating LLM instance...")
llm = ConcreteHuggingChatLLM()
print("Instance created. Asking LLM...")
# Attempt to use the ConcreteHuggingChatLLM
response = llm("Hello, how are you?") # directly using the __call__ method
print("LLM responded:", response)
Key Updates:
We're now making sure to print status messages throughout the process to observe how far the script runs before encountering an issue.
Updated the __init__ -> Correctly initiating the superclass if needed; in your snippet, direct superclass initiation is missing; ensure this aligns with how LLM expects to be extended.
Incorporated the __call__ method allowing the class instance to be called directly, making it respond to prompt inputs as intended.
Adding output for the login phase and upon initialization of the chatbot might pinpoint if the problem exists in the setup phase or when invoking the LLM.
Remember, the actual problem might be elsewhere depending on if your external service works as expected, your .env file setup, network conditions, or other unseen script miscues. These updates aim to make your script's flow more transparent and debuggable.
self.id: str = id
self.title: str = title
self.model = model
self.system_prompt: str = system_prompt
self.history: list = history
def __str__(self) -> str:
return self.id
we need to keep this for backward compatibility, because some people may use it.
conversation = Conversation
model = Model
class ChatBot:
cookies: dict
"""Cookies for authentication"""
code
session: Session
"""HuggingChat session"""
def __init__(
self,
cookies: Union[dict, None, RequestsCookieJar] = None,
cookie_path: str = "",
default_llm: Union[int, str] = 0,
system_prompt: str = "",
) -> None:
"""
Returns a ChatBot object
default_llm: name or index
"""
if cookies is None and cookie_path == "":
raise exceptions.ChatBotInitError(
"Authentication is required now, but no cookies provided. See tutorial at https://github.com/Soulter/hugging-chat-api"
)
elif cookies is not None and cookie_path != "":
raise exceptions.ChatBotInitError("Both cookies and cookie_path provided")
if cookies is None and cookie_path != "":
# read cookies from path
if not os.path.exists(cookie_path):
raise exceptions.ChatBotInitError(
f"Cookie file {cookie_path} not found. Note: The file must be in JSON format and must contain a list of cookies. See more at https://github.com/Soulter/hugging-chat-api"
)
with open(cookie_path, "r", encoding="utf-8") as f:
cookies = json.load(f)
# convert cookies to KV format
if isinstance(cookies, list):
cookies = {cookie["name"]: cookie["value"] for cookie in cookies}
self.cookies = cookies
self.hf_base_url = "https://huggingface.co"
self.json_header = {"Content-Type": "application/json"}
self.session = self.get_hc_session()
self.conversation_list = []
self.accepted_welcome_modal = (
False # It is no longer required to accept the welcome modal
)
self.llms = self.get_remote_llms()
if isinstance(default_llm, str):
self.active_model = self.get_llm_from_name(default_llm)
if self.active_model is None:
raise Exception(
f"Given model is not in llms list. LLM list: {[model.id for model in self.llms]}"
)
else:
self.active_model = self.llms[default_llm]
self.current_conversation = self.new_conversation(system_prompt=system_prompt)
def get_hc_session(self) -> Session:
session = Session()
# set cookies
session.cookies.update(self.cookies)
session.get(self.hf_base_url + "/chat")
return session
def get_headers(self, ref=True, ref_cid: Conversation = None) -> dict:
_h = {
"Accept": "*/*",
"Connection": "keep-alive",
"Host": "huggingface.co",
"Origin": "https://huggingface.co",
"Sec-Fetch-Site": "same-origin",
"Content-Type": "application/json",
"Sec-Ch-Ua-Platform": "Windows",
"Sec-Ch-Ua": 'Chromium";v="116", "Not)A;Brand";v="24", "Microsoft Edge";v="116',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
}
if ref:
if ref_cid is None:
ref_cid = self.current_conversation
_h["Referer"] = f"https://huggingface.co/chat/conversation/{ref_cid}"
return _h
def get_cookies(self) -> dict:
return self.session.cookies.get_dict()
# NOTE: To create a copy when calling this, call it inside of list().
# If not, when updating or altering the values in the variable will
# also be applied to this class's variable.
# This behavior is with any function returning self.<var_name>. It
# acts as a pointer to the data in the object.
#
# Returns a pointer to this objects list that contains id of conversations.
def get_conversation_list(self) -> list:
return list(self.conversation_list)
def get_active_llm_index(self) -> int:
return self.llms.index(self.active_model)
def accept_ethics_modal(self):
"""
[Deprecated Method]
"""
response = self.session.post(
self.hf_base_url + "/chat/settings",
headers=self.get_headers(ref=False),
cookies=self.get_cookies(),
allow_redirects=True,
data={
"ethicsModalAccepted": "true",
"shareConversationsWithModelAuthors": "true",
"ethicsModalAcceptedAt": "",
"activeModel": str(self.active_model),
},
)
if response.status_code != 200:
raise Exception(
f"Failed to accept ethics modal with status code: {response.status_code}. {response.content.decode()}"
)
return True
def new_conversation(
self,
modelIndex: int = None,
system_prompt: str = "",
switch_to: bool = False,
assistant: Union[str, Assistant] = None,
) -> Conversation:
"""
Create a new conversation. Return a conversation object.
modelIndex: int, get it from get_available_llm_models(). If None, use the default model.
assistant: str or Assistant, the assistant **id** or assistant object. Use search_assistant() to get the assistant object.
- You should change the conversation by calling change_conversation() after calling this method. Or set param switch_to to True.
- if you use assistant, the parameter `system_prompt` will be ignored.
"""
err_count = 0
if modelIndex is None:
model = self.active_model
else:
if modelIndex < 0 or modelIndex >= len(self.llms):
raise IndexError("Out of range of llm index")
model = self.llms[modelIndex]
# Accept the welcome modal when init.
# 17/5/2023: This is not required anymore.
# if not self.accepted_welcome_modal:
# self.accept_ethics_modal()
# Create new conversation and get a conversation id.
_header = self.get_headers(ref=False)
_header["Referer"] = "https://huggingface.co/chat"
request = {
"model": model.id,
}
# get assistant id
if assistant is not None:
assistant_id = None
if isinstance(assistant, str):
assistant_id = assistant
elif isinstance(assistant, Assistant):
assistant_id = assistant.assistant_id
else:
raise ValueError("param assistant must be a string or Assistant object.")
request["assistantId"] = assistant_id
else:
request["preprompt"] = system_prompt if system_prompt != "" else model.preprompt
while True:
try:
resp = self.session.post(
self.hf_base_url + "/chat/conversation",
json=request,
headers=_header,
cookies=self.get_cookies(),
)
logging.debug(resp.text)
cid = json.loads(resp.text)["conversationId"]
c = Conversation(id=cid, system_prompt=system_prompt, model=model)
self.conversation_list.append(c)
if switch_to:
self.change_conversation(c)
# we need know the root message id (a.k.a system prompt message id).
self.get_conversation_info(c)
return c
except BaseException as e:
err_count += 1
logging.debug(
f" Failed to create new conversation ({e}). Retrying... ({err_count})"
)
if err_count > 5:
raise exceptions.CreateConversationError(
f"Failed to create new conversation with status code: {resp.status_code}. Error: {e}. Retries: {err_count}."
)
continue
def change_conversation(self, conversation_object: Conversation):
"""
Change the current conversation to another one.
"""
local_conversation = self.get_conversation_from_id(conversation_object.id)
if local_conversation is None:
raise exceptions.InvalidConversationIDError(
"Invalid conversation id, not in conversation list."
)
self.get_conversation_info(local_conversation)
self.current_conversation = local_conversation
def share_conversation(self, conversation_object: Conversation = None) -> str:
"""
Return a share link of the conversation.
"""
if conversation_object is None:
conversation_object = self.current_conversation
headers = self.get_headers()
r = self.session.post(
f"{self.hf_base_url}/chat/conversation/{conversation_object}/share",
headers=headers,
cookies=self.get_cookies(),
)
if r.status_code != 200:
raise Exception(
f"Failed to share conversation with status code: {r.status_code}"
)
response = r.json()
if "url" in response:
return response["url"]
raise Exception(f"Unknown server response: {response}")
def delete_all_conversations(self) -> None:
"""
Deletes ALL conversations on the HuggingFace account
"""
settings = {"": ("", "")}
r = self.session.post(
f"{self.hf_base_url}/chat/conversations?/delete",
headers={"Referer": "https://huggingface.co/chat"},
cookies=self.get_cookies(),
allow_redirects=True,
files=settings,
)
if r.status_code != 200:
raise exceptions.DeleteConversationError(
f"Failed to delete ALL conversations with status code: {r.status_code}"
)
self.conversation_list = []
self.current_conversation = None
def delete_conversation(self, conversation_object: Conversation = None) -> None:
"""
Delete a HuggingChat conversation by conversation.
"""
if conversation_object is None:
conversation_object = self.current_conversation
headers = self.get_headers()
r = self.session.delete(
f"{self.hf_base_url}/chat/conversation/{conversation_object}",
headers=headers,
cookies=self.get_cookies(),
)
if r.status_code != 200:
raise exceptions.DeleteConversationError(
f"Failed to delete conversation with status code: {r.status_code}"
)
else:
self.conversation_list.pop(
self.get_conversation_from_id(conversation_object.id, return_index=True)
)
if conversation_object is self.current_conversation:
self.current_conversation = None
def get_available_llm_models(self) -> list:
"""
Get all available models that are available in huggingface.co/chat.
"""
return self.llms
def set_share_conversations(self, val: bool = True):
"""
Sets the "Share Conversation with Model Authors setting" to the given val variable
"""
settings = {"shareConversationsWithModelAuthors": ("", "on" if val else "")}
r = self.session.post(
self.hf_base_url + "/chat/settings",
headers={"Referer": "https://huggingface.co/chat"},
cookies=self.get_cookies(),
allow_redirects=True,
files=settings,
)
if r.status_code != 200:
raise Exception(
f"Failed to set share conversation with status code: {r.status_code}"
)
def switch_llm(self, index: int) -> bool:
"""
Attempts to change current conversation's Large Language Model.
Requires an index to indicate the model you want to switch.
See self.llms for available models.
Note: 1. The effect of switch is limited to the current conversation,
You can manually switch the llm when you start a new conversation.
2. Only works *after creating a new conversation.*
:)
"""
# TODO: I will work on making it have a model for each conversation that is changeable. - @Zekaroni
if index < len(self.llms) and index >= 0:
self.active_model = self.llms[index]
return True
else:
raise IndexError("Out of range of llm index")
def get_llm_from_name(self, name: str) -> Union[Model, None]:
for model in self.llms:
if model.name == name:
return model
# Gives information such as name, websiteUrl, description, displayName, parameters, etc.
# We can use it in the future if we need to get information about models
def get_remote_llms(self) -> list:
"""
Fetches all possible LLMs that could be used. Returns the LLMs in a list
"""
r = self.session.post(
self.hf_base_url + "/chat/__data.json",
headers=self.get_headers(ref=False),
cookies=self.get_cookies(),
)
if r.status_code != 200:
raise Exception(
f"Failed to get remote LLMs with status code: {r.status_code}"
)
data = r.json()["nodes"][0]["data"]
modelsIndices = data[data[0]["models"]]
model_list = []
return_data_from_index = lambda index: None if index == -1 else data[index]
for modelIndex in modelsIndices:
model_data = data[modelIndex]
# Model is unlisted, skip it
if data[model_data["unlisted"]]:
continue
m = Model(
id=return_data_from_index(model_data["id"]),
name=return_data_from_index(model_data["name"]),
displayName=return_data_from_index(model_data["displayName"]),
preprompt=return_data_from_index(model_data["preprompt"]),
# promptExamples = return_data_from_index(model_data["promptExamples"]),
websiteUrl=return_data_from_index(model_data["websiteUrl"]),
description=return_data_from_index(model_data["description"]),
datasetName=return_data_from_index(model_data["datasetName"]),
datasetUrl=return_data_from_index(model_data["datasetUrl"]),
modelUrl=return_data_from_index(model_data["modelUrl"]),
# parameters = return_data_from_index(model_data["parameters"]),
)
prompt_list = return_data_from_index(model_data["promptExamples"])
if prompt_list is not None:
_promptExamples = [
return_data_from_index(index) for index in prompt_list
]
m.promptExamples = [
{"title": data[prompt["title"]], "prompt": data[prompt["prompt"]]}
for prompt in _promptExamples
]
indices_parameters_dict = return_data_from_index(model_data["parameters"])
out_parameters_dict = {}
for key, value in indices_parameters_dict.items():
if value == -1:
out_parameters_dict[key] = None
continue
if isinstance(type(data[value]), list):
out_parameters_dict[key] = [data[index] for index in data[value]]
continue
out_parameters_dict[key] = data[value]
m.parameters = out_parameters_dict
model_list.append(m)
return model_list
def get_remote_conversations(self, replace_conversation_list=True):
"""
Returns all the remote conversations for the active account. Returns the conversations in a list.
"""
r = self.session.post(
self.hf_base_url + "/chat/__data.json",
headers=self.get_headers(ref=False),
cookies=self.get_cookies(),
)
if r.status_code != 200:
raise Exception(
f"Failed to get remote conversations with status code: {r.status_code}"
)
data = r.json()["nodes"][0]["data"]
conversationIndices = data[data[0]["conversations"]]
conversations = []
for index in conversationIndices:
conversation_data = data[index]
c = Conversation(
id=data[conversation_data["id"]],
title=data[conversation_data["title"]],
model=data[conversation_data["model"]],
)
conversations.append(c)
if replace_conversation_list:
self.conversation_list = conversations
return conversations
def get_conversation_info(self, conversation: Conversation = None) -> Conversation:
"""
Fetches information related to the specified conversation. Returns the conversation object.
"""
if conversation is None:
conversation = self.current_conversation
r = self.session.post(
self.hf_base_url + f"/chat/conversation/{conversation.id}/__data.json",
headers=self.get_headers(ref=False),
cookies=self.get_cookies(),
)
if r.status_code != 200:
raise Exception(
f"Failed to get conversation info with status code: {r.status_code}"
)
data = r.json()["nodes"][1]["data"]
conversation.model = data[data[0]["model"]]
conversation.system_prompt = data[data[0]["preprompt"]]
conversation.title = data[data[0]["title"]]
messages: list = data[data[0]["messages"]]
conversation.history = []
# parse all message nodes (history) in the conversation
for index in messages: # node's index
_node_meta = data[index]
conversation.history.append(MessageNode(
id=data[_node_meta["id"]],
role=data[_node_meta["from"]],
content=data[_node_meta["content"]],
created_at=datetime.datetime.strptime(data[_node_meta["createdAt"]][1], "%Y-%m-%dT%H:%M:%S.%fZ").timestamp(),
updated_at=datetime.datetime.strptime(data[_node_meta["updatedAt"]][1], "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
))
logging.debug(f"conversation {conversation.id} history: {conversation.history}")
return conversation
def get_conversation_from_id(self, conversation_id: str, return_index=False) -> Conversation:
"""
Returns a conversation object that is already in the conversation list.
"""
for i, conversation in enumerate(self.conversation_list):
if conversation.id == conversation_id:
if return_index:
return i
return conversation
def _parse_assistants(self, nodes_data: list) -> List[Assistant]:
'''
parse the assistants data from the response.
'''
index = nodes_data[1]
ret = []
for i in index:
attribute_map: dict = nodes_data[i]
assistant_id = nodes_data[attribute_map['_id']]
author = nodes_data[attribute_map['createdByName']]
name = nodes_data[attribute_map['name']].strip()
model_name = nodes_data[attribute_map['modelId']]
pre_prompt = nodes_data[attribute_map['preprompt']]
description = nodes_data[attribute_map['description']]
ret.append(Assistant(
assistant_id,
author,
name,
model_name,
pre_prompt,
description
))
return ret
def get_assistant_list_by_page(self, page: int) -> List[Assistant]:
'''
get assistant list by page number.
if page < 0 or page > max_page then return `None`.
'''
url_cache = f"https://api.soulter.top/hugchat/assistants/__data.json?p={page}"
url = f"https://huggingface.co/chat/assistants/__data.json?p={page}&x-sveltekit-invalidated=01"
try:
res = requests.get(url_cache, timeout=5)
except BaseException:
res = self.session.get(url, timeout=10)
res = res.json()
if res['nodes'][1]['type'] == 'error':
return None
# here we parse the result
return self._parse_assistants(res['nodes'][1]['data'])
def search_assistant(self, assistant_name: str = None, assistant_id: str = None) -> Assistant:
'''
- Search an available assistant by assistant name or assistant id.
- Will search on api.soulter.top/hugchat because offifial api doesn't support search.
- Return the `Assistant` object if found, return None if not found.
'''
if not assistant_name and not assistant_id:
raise ValueError("assistant_name and assistant_id can not be both None.")
if assistant_name:
url = f"https://api.soulter.top/hugchat/assistant?name={assistant_name}"
else:
url = f"https://api.soulter.top/hugchat/assistant?id={assistant_id}"
res = requests.get(url, timeout=10)
if res.status_code != 200:
raise Exception(f"Failed to search assistant with status code: {res.status_code}, please commit an issue to https://github.com/Soulter/hugging-chat-api/issues")
res = res.json()
if not res['data']:
# empty dict
return None
if res['code'] != 0:
raise Exception(f"Failed to search assistant with server's error: {res['message']}, please commit an issue to https://github.com/Soulter/hugging-chat-api/issues")
return Assistant(**res['data'])
def _stream_query(
self,
text: str,
web_search: bool = False,
temperature: float = 0.1,
top_p: float = 0.95,
repetition_penalty: float = 1.2,
top_k: int = 50,
truncate: int = 1000,
watermark: bool = False,
max_new_tokens: int = 1024,
stop: list = ["</s>"],
return_full_text: bool = False,
use_cache: bool = False,
is_retry: bool = False,
retry_count: int = 5,
_stream_yield_all: bool = False, # yield all responses from the server.
conversation: Conversation = None,
) -> typing.Generator[dict, None, None]:
if conversation is None:
conversation = self.current_conversation
if retry_count <= 0:
raise Exception("the parameter retry_count must be greater than 0.")
if text == "":
raise Exception("the prompt can not be empty.")
if len(conversation.history) == 0:
raise Exception("conversation history is empty, but we need the root message id of this conversation to continue.")
# get last message id
last_assistant_message = conversation.history[-1]
logging.debug(f"conversation {conversation.id} last message id: {last_assistant_message.id}")
req_json = {
"files": [],
"id": last_assistant_message.id,
"inputs": text,
"is_continue": False,
"is_retry": is_retry,
"web_search": web_search,
}
headers = {
'authority': 'huggingface.co',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6',
'content-type': 'application/json',
'origin': 'https://huggingface.co',
'sec-ch-ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
}
final_answer = {}
break_flag = False
while retry_count > 0:
resp = self.session.post(
self.hf_base_url + f"/chat/conversation/{conversation}",
json=req_json,
stream=True,
headers=headers,
cookies=self.session.cookies.get_dict(),
)
resp.encoding='utf-8'
if resp.status_code != 200:
retry_count -= 1
if retry_count <= 0:
raise exceptions.ChatError(f"Failed to chat. ({resp.status_code})")
try:
for line in resp.iter_lines(decode_unicode=True):
if not line:
continue
res = line
obj = json.loads(res)
if obj.__contains__("type"):
_type = obj["type"]
if _type == "finalAnswer":
final_answer = obj
break_flag = True
break
else:
logging.error(f"No `type` found in response: {obj}")
yield obj
except requests.exceptions.ChunkedEncodingError:
pass
except BaseException as e:
traceback.print_exc()
if "Model is overloaded" in str(e):
raise exceptions.ModelOverloadedError(
"Model is overloaded, please try again later or switch to another model."
)
logging.debug(resp.headers)
raise exceptions.ChatError(f"Failed to parse response: {res}")
if break_flag:
break
# update the history of current conversation
self.get_conversation_info(conversation)
yield final_answer
def query(
self,
text: str,
web_search: bool = False,
temperature: float = 0.1,
top_p: float = 0.95,
repetition_penalty: float = 1.2,
top_k: int = 50,
truncate: int = 1000,
watermark: bool = False,
max_new_tokens: int = 1024,
stop: list = ["</s>"],
return_full_text: bool = False,
stream: bool = False,
_stream_yield_all: bool = False, # For stream mode, yield all responses from the server.
use_cache: bool = False,
is_retry: bool = False,
retry_count: int = 5,
conversation: Conversation = None,
) -> Message:
"""
**Deprecated**
Same as chat now
"""
if conversation is None:
conversation = self.current_conversation
return self.chat(
text=text,
web_search=web_search,
_stream_yield_all=_stream_yield_all,
retry_count=retry_count,
conversation=conversation,
)
def chat(
self,
text: str,
web_search: bool = False,
_stream_yield_all: bool = False, # For stream mode, yield all responses from the server.
retry_count: int = 5,
conversation: Conversation = None,
*args,
**kvargs,
) -> Message:
"""
Send a message to the current conversation. Return a Message object.
You can turn on the web search by set the parameter `web_search` to True
Stream is now the default mode, you can call Message.wait_until_done()
About class `Message`:
- `wait_until_done()`: Block until the response done processing or an error raised.
- `__iter__()`: For loop call this Generator and get response.
- `get_search_sources()`: The web search results. It is a list of WebSearchSource objects.
For more detail please see Message documentation(Message.__doc__)
"""
if conversation is None:
conversation = self.current_conversation
msg = Message(
g=self._stream_query(
text=text,
web_search=web_search,
_stream_yield_all=_stream_yield_all, # For stream mode, yield all responses from the server.
retry_count=retry_count,
conversation=conversation,
),
_stream_yield_all=_stream_yield_all,
web_search=web_search,
)
return msg
if name == "main":
bot = ChatBot()
message_content = bot.chat("Hello")
print(message_content)
sharelink = bot.share_conversation()
print(sharelink)
"
3- file 3 : from .message import Message
"from typing import Generator, Union
from .exceptions import ChatError, ModelOverloadedError
import json
A wrapper of `Generator` that receives and process the response
:Example:
.. code-block:: python
msg = bot.chat(...)
# stream process
for res in msg:
... # process
else:
if msg.done() == MSGSTATUS_REJECTED:
raise msg.error
# or simply use:
final = msg.wait_until_done()
"""
g: Generator
_stream_yield_all: bool = False
web_search: bool = False
web_search_sources: list = []
text: str = "" # For backward compatibility, we have to reserve the `text` field.
web_search_done: bool = not web_search
msg_status: int = MSGSTATUS_PENDING
error: Union[Exception, None] = None
def __init__(
self,
g: Generator,
_stream_yield_all: bool = False,
web_search: bool = False,
) -> None:
self.g = g
self._stream_yield_all = _stream_yield_all
self.web_search = web_search
def _filterResponse(self, obj: dict):
if not obj.__contains__("type"):
if obj.__contains__("message"):
raise ChatError(f"Server returns an error: {obj['message']}")
else:
raise ChatError(f"No `type` and `message` returned: {obj}")
def __next__(self) -> dict:
if self.msg_status == MSGSTATUS_RESOLVED:
raise StopIteration
elif self.msg_status == MSGSTATUS_REJECTED:
if self.error is not None:
raise self.error
else:
raise Exception(
"Message stauts is `Rejected` but no error found")
try:
a: dict = next(self.g)
self._filterResponse(a)
t: str = a["type"]
message_type: str = ""
if t == RESPONSE_TYPE_FINAL:
self.text = a["text"]
self.msg_status = MSGSTATUS_RESOLVED
elif t == RESPONSE_TYPE_WEB:
# gracefully pass unparseable webpages
if message_type != MSGTYPE_ERROR and a.__contains__("sources"):
self.web_search_sources.clear()
sources = a["sources"]
for source in sources:
wss = WebSearchSource()
wss.title = source["title"]
wss.link = source["link"]
wss.hostname = source["hostname"]
self.web_search_sources.append(wss)
elif "messageType" in a:
message_type: str = a["messageType"]
if message_type == MSGTYPE_ERROR:
self.error = ChatError(a["message"])
self.msg_status = MSGSTATUS_REJECTED
if t == RESPONSE_TYPE_STREAM:
self.web_search_done = True
elif t == RESPONSE_TYPE_STATUS:
pass
else:
if "Model is overloaded" in str(a):
self.error = ModelOverloadedError(
"Model is overloaded, please try again later or switch to another model."
)
self.msg_status = MSGSTATUS_REJECTED
elif a.__contains__(MSGTYPE_ERROR):
self.error = ChatError(a[MSGTYPE_ERROR])
self.msg_status = MSGSTATUS_REJECTED
else:
self.error = ChatError(f"Unknown json response: {a}")
# If _stream_yield_all is True, yield all responses from the server.
if self._stream_yield_all or t == RESPONSE_TYPE_STREAM:
return a
else:
return self.__next__()
except StopIteration:
if self.msg_status == MSGSTATUS_PENDING:
self.error = ChatError(
"Stream of responses has abruptly ended (final answer has not been received)."
)
raise self.error
pass
except Exception as e:
# print("meet error: ", str(e))
self.error = e
self.msg_status = MSGSTATUS_REJECTED
raise self.error
def __iter__(self):
return self
def throw(
self,
__typ,
__val=None,
__tb=None,
):
return self.g.throw(__typ, __val, __tb)
def send(self, __value):
return self.g.send(__value)
def get_final_text(self) -> str:
"""
:Return:
- self.text
"""
return self.text
def get_search_sources(self) -> list:
"""
:Return:
- self.web_search_sources
"""
return self.web_search_sources
def search_enabled(self) -> bool:
"""
:Return:
- self.web_search
"""
return self.web_search
def wait_until_done(self) -> str:
"""
:Return:
- self.text if resolved else raise error
wait until every response is resolved
"""
while not self.is_done():
self.__next__()
if self.is_done() == MSGSTATUS_RESOLVED:
return self.text
elif self.error is not None:
raise self.error
else:
raise Exception("Rejected but no error captured!")
def is_done(self):
"""
:Return:
- self.msg_status
3 status:
- MSGSTATUS_PENDING = 0 # running
- MSGSTATUS_RESOLVED = 1 # done with no error(maybe?)
- MSGSTATUS_REJECTED = 2 # error raised
"""
return self.msg_status
def is_done_search(self):
"""
:Return:
- self.web_search_done
web search result will be set to `done` once the token is received
"""
return self.web_search_done
def __str__(self):
return self.wait_until_done()
def __getitem__(self, key: str) -> str:
print("_getitem_")
self.wait_until_done()
print("done")
if key == "text":
return self.text
elif key == "web_search":
return self.web_search
elif key == "web_search_sources":
return self.web_search_sources
def __add__(self, other: str) -> str:
self.wait_until_done()
return self.text + other
def __radd__(self, other: str) -> str:
self.wait_until_done()
return other + self.text
def __iadd__(self, other: str) -> str:
self.wait_until_done()
self.text += other
return self.text
if name == "main":
pass
"
4-file 4: from . import exceptions
"class ModelOverloadedError(Exception):
"""
HF Model Overloaded Error
code
Raised when hf return response `{"error":"Model is overloaded", "error_type":"overloaded"}`
"""
pass
class ChatBotInitError(Exception):
"""
ChatBot Init Error
code
Raised when chatbot init failed
"""
pass
class CreateConversationError(Exception):
"""
Create Conversation Error
code
Raised when create conversation failed
"""
pass
class InvalidConversationIDError(Exception):
"""
Invalid Conversation ID Error
code
Raised when using a invalid conversation id
"""
pass
class DeleteConversationError(Exception):
"""
Delete Conversation Error
code
Raised when delete conversation failed
"""
pass
class ChatError(Exception):
"""
Chat Error
code
Raised when chat failed
"""
pass
"
from langchain.language_models.llms import LLM # Adjust import path if necessary
from hugchat.chatbot import ChatBot # Adjust import path if necessary
from typing import Optional, List
from dotenv import load_dotenv
import os
class ConcreteHuggingChatLLM(LLM):
def __init__(self, cookies: dict, model_index=0):
self.chatbot = ChatBot(cookies=cookies)
self.chatbot.switch_llm(model_index)
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
message = self.chatbot.chat(prompt)
return message.wait_until_done()
if __name__ == "__main__":
load_dotenv()
hf_email = os.getenv("HF_EMAIL")
hf_password = os.getenv("HF_PASSWORD")
# Assume Login class handles authentication and returns a dictionary of cookies.
login_service = Login(hf_email, hf_password)
cookies = login_service.login()
llm = ConcreteHuggingChatLLM(cookies=cookies.get_dict())
response = llm("Hello, how are you?")
print(response)
Key Integration Steps:
Authentication: Uses Login service to authenticate against Hugging Chat API and retrieve necessary cookies.
Concrete LLM: The ConcreteHuggingChatLLM class inherits from LLM and composes a ChatBot instance for actual chat operations.
LLM Calls: Through overriding _call, we forward chat prompts to the encapsulated ChatBot, handling response fetching seamlessly.
Adjustments and Considerations:
Ensure Login and ChatBot classes are correctly imported from their respective modules.
The cookies mechanism derived from Login service must align with ChatBot's expectation.
Handle environment-specific configurations, such as model indices or additional ChatBot settings, as per your application's requirements.
Extend error handling based on the provided exceptions to manage failures gracefully.
This implementation serves as a foundational blueprint. Depending on the depth of integration and customization needed, further adjustments might be necessary, especially around error management, environment setup, and nuanced control over conversation or model switching logic.