emirsahin1 / llm-axe

A simple, intuitive toolkit for quickly implementing LLM powered applications.
MIT License
67 stars 14 forks source link

AttributeError: module 'collections' has no attribute 'Callable' #2

Closed RaumwerkVogler closed 1 month ago

RaumwerkVogler commented 1 month ago

Each time I try to get a answer out of the www I get the following traceback;

Traceback (most recent call last): File "c:\Users\info\OneDrive\Desktop\projektmanagement\ollama web.py", line 85, in main() File "c:\Users\info\OneDrive\Desktop\projektmanagement\ollama web.py", line 76, in main resp = online_agent.search(user_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\llm_axe\agents.py", line 535, in search search_results = self.search_function(query) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\llm_axe\core.py", line 157, in internet_search urls = list(search(query, tld="co.in", num=5, stop=10, pause=2)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\googlesearch__init.py", line 309, in search soup = BeautifulSoup(html, 'html.parser') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4__init.py", line 215, in init__ self._feed() File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4__init__.py", line 239, in _feed self.builder.feed(self.markup) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\builder_htmlparser.py", line 164, in feed parser.feed(markup) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 110, in feed self.goahead(0) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 178, in goahead k = self.parse_html_declaration(i) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\html\parser.py", line 269, in parse_html_declaration self.handle_decl(rawdata[i+2:gtpos]) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\builder_htmlparser.py", line 109, in handle_decl self.soup.endData(Doctype) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\init.py", line 315, in endData self.object_was_parsed(o) File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\init.py", line 320, in object_was_parsed previous_element = most_recent_element or self._most_recent_element ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 997, in getattr return self.find(tag) ^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 1234, in find l = self.find_all(name, attrs, recursive, text, 1, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 1255, in find_all return self._find_all(name, attrs, text, limit, generator, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 512, in _find_all strainer = SoupStrainer(name, attrs, text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 1548, in init__ self.text = self._normalize_search_value(text) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\info\AppData\Local\Programs\Python\Python311\Lib\site-packages\bs4\element.py", line 1553, in _normalize_search_value if (isinstance(value, str) or isinstance(value, collections.Callable) or hasattr(value, 'match') ^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'collections' has no attribute 'Callable'

Used your democode after getting errors with the code from your video on youtube:

Example Conversation Output:

User: Who are you?

AI: I am a helpful chat assistant.

User: Great. Can you find me a website that I can use to practice web scraping?

AI: Based on information from the internet, one such website is https://www.scrapethissite.com/pages/.

This website provides various examples of pages for practicing web scraping, including country lists, hockey team stats, Oscar-winning films, and more. These examples cover different types of interfaces, such as forms, searching, pagination, AJAX, JavaScript, frames, and iFrames, making it a great resource to practice your web scraping skills.

User: Thanks!

AI: You're welcome!

This is an example chat session through the terminal

The AI will use the internet if it is required, otherwise, it will use its own knowledge base

from llm_axe import make_prompt, AgentType, OllamaChat, OnlineAgent, FunctionCaller, Agent

def internet(): """Choose if the internet is required or if we don't have information about the topic""" return ""

def no_internet(): """Choose if the internet is not required""" return ""

prompt = ''' You are an AI assistant. Determine whether or not the internet is required to answer the user's prompt. If the internet is not required, answer "no internet required". If the internet is required, answer "internet required".

    Internet is required if you don't have information about the topic and the user's prompt is talking about something SPECIFIC that we need solid FACTS for.
    Internet is not required if you believe that the internet is not required to correctly answer the user's question.

    Do not respond with anything else.

'''

def main(): print('''


    Welcome to the online chat demo. 
    If the AI is unable to answer your question, it will use the internet to answer.
    Otherwise, it will answer using its own knowledge base.
    Type 'exit' to exit.
    ******************************************************************''')

llm = OllamaChat(model="llama3:instruct")
online_agent = OnlineAgent(llm)
plan_agent = Agent(llm, custom_system_prompt=prompt)
normal_agent = Agent(llm, agent_type=AgentType.GENERIC_RESPONDER)
function_caller = FunctionCaller(llm, functions=[internet, no_internet])

chat_history = []
user_input = ""

while True:
    print("User: ", end="")
    user_input = input()
    if user_input == "exit":
        exit()

    chat_history.append(make_prompt("user", user_input))

    # First determine if the internet is required or not
    plan = plan_agent.ask("USERS INPUT: " + user_input)

    function_name = "no_internet"
    func_resp = function_caller.get_function(plan) 
    if func_resp is not None:
        function_name = func_resp["function"].__name__

    resp = ""
    if function_name == "internet":
        resp = online_agent.search(user_input)
    else:
        resp = normal_agent.ask(user_input, history=chat_history)

    print("\nAI: " + resp + "\n")
    chat_history.append(make_prompt("assistant", resp))

if name == 'main': main()

Do you have any idea, what`s going wrong on my side?
emirsahin1 commented 1 month ago

Hi. Thanks for reaching out about this.

This seems like an issue regarding the version of beautifulsoup4. Could you please let me know what version of beautifulsoup4 and Python you are using? Thanks!

RaumwerkVogler commented 1 month ago

Hey, thanks for your fast reaction.

bf4 seams to be 4.4.0 python is the 3.11.4

sorry for my unnecessary question. It was simply because I didn't pay attention to the reqirements.txt and didn't adjust the system. After I installed the requirements.txt everything works fine.

Great job. It will make my life much more easy. Thanks for this repo :-)