devika icon indicating copy to clipboard operation
devika copied to clipboard

Error: Can any one help?

Open csaicharan opened this issue 2 years ago • 6 comments

Describe the bug issue while running devika.py

24.04.04 07:41:04: root: INFO : Initializing Devika... 24.04.04 07:41:04: root: INFO : Initializing Prerequisites Jobs... 24.04.04 07:41:10: root: INFO : Loading sentence-transformer BERT models... 24.04.04 07:41:14: root: INFO : BERT model loaded successfully. 24.04.04 07:41:15: root: INFO : Ollama available Traceback (most recent call last): File ".\devika.py", line 26, in from src.agents import Agent File "E:\Workspace\Research\Projects\devika\src\agents_init_.py", line 1, in from .agent import Agent File "E:\Workspace\Research\Projects\devika\src\agents\agent.py", line 2, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher_init_.py", line 1, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 12, in class Researcher: File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 25, in Researcher def validate_response(self, response: str) -> dict | bool: TypeError: unsupported operand type(s) for |: 'type' and 'type' PS E:\Workspace\Research\Projects\devika> python3 .\devika.py 24.04.04 07:42:55: root: INFO : Initializing Devika... 24.04.04 07:42:55: root: INFO : Initializing Prerequisites Jobs... 24.04.04 07:42:59: root: INFO : Loading sentence-transformer BERT models... 24.04.04 07:43:02: root: INFO : BERT model loaded successfully. 24.04.04 07:43:03: root: INFO : Ollama available Traceback (most recent call last): File ".\devika.py", line 26, in from src.agents import Agent File "E:\Workspace\Research\Projects\devika\src\agents_init_.py", line 1, in from .agent import Agent File "E:\Workspace\Research\Projects\devika\src\agents\agent.py", line 2, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher_init_.py", line 1, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 12, in class Researcher: File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 25, in Researcher def validate_response(self, response: str) -> dict | bool: TypeError: unsupported operand type(s) for |: 'type' and 'type' PS E:\Workspace\Research\Projects\devika> python3 .\devika.py 24.04.04 07:44:50: root: INFO : Initializing Devika... 24.04.04 07:44:50: root: INFO : Initializing Prerequisites Jobs... 24.04.04 07:44:54: root: INFO : Loading sentence-transformer BERT models... 24.04.04 07:44:57: root: INFO : BERT model loaded successfully. 24.04.04 07:44:58: root: INFO : Ollama available Traceback (most recent call last): File ".\devika.py", line 26, in from src.agents import Agent File "E:\Workspace\Research\Projects\devika\src\agents_init_.py", line 1, in from .agent import Agent File "E:\Workspace\Research\Projects\devika\src\agents\agent.py", line 2, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher_init_.py", line 1, in from .researcher import Researcher File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 12, in class Researcher: File "E:\Workspace\Research\Projects\devika\src\agents\researcher\researcher.py", line 25, in Researcher def validate_response(self, response: str) -> dict | bool: TypeError: unsupported operand type(s) for |: 'type' and 'type'

csaicharan avatar Apr 04 '24 05:04 csaicharan

I got the same error

darrassi1 avatar Apr 04 '24 06:04 darrassi1

Follow the Bug Report template for reporting errors, as it guarantees a better understanding and readability for fellow users and developers.

MrAnayDongre avatar Apr 04 '24 06:04 MrAnayDongre

Upgrade python version to python 3.10 or 3.11

IemProg avatar Apr 04 '24 06:04 IemProg

upgrading python version is not working

Abhishekrajpurohit avatar Apr 04 '24 06:04 Abhishekrajpurohit

Change the code in the file path 'src/agents/researcher/researcher.py' to this code

import json
from typing import List
from jinja2 import Environment, BaseLoader
from typing import Union
from src.llm import LLM
from src.browser.search import BingSearch

PROMPT = open("src/agents/researcher/prompt.jinja2").read().strip()


class Researcher:
    def __init__(self, base_model: str):
        self.bing_search = BingSearch()
        self.llm = LLM(model_id=base_model)

    def render(self, step_by_step_plan: str, contextual_keywords: str) -> str:
        env = Environment(loader=BaseLoader())
        template = env.from_string(PROMPT)
        return template.render(
            step_by_step_plan=step_by_step_plan,
            contextual_keywords=contextual_keywords
        )

    def validate_response(self, response: str) -> Union[dict , bool]:
        response = response.strip().replace("```json", "```")

        if response.startswith("```") and response.endswith("```"):
            response = response[3:-3].strip()
        try:
            response = json.loads(response)
        except Exception as _:
            return False

        response = {k.replace("\\", ""): v for k, v in response.items()}

        if "queries" not in response and "ask_user" not in response:
            return False
        else:
            return {
                "queries": response["queries"],
                "ask_user": response["ask_user"]
            }

    def execute(self, step_by_step_plan: str, contextual_keywords: List[str], project_name: str) -> Union[dict , bool]:
        contextual_keywords_str = ", ".join(map(lambda k: k.capitalize(), contextual_keywords))
        prompt = self.render(step_by_step_plan, contextual_keywords_str)
        
        response = self.llm.inference(prompt, project_name)
        
        valid_response = self.validate_response(response)

        while not valid_response:
            print("Invalid response from the model, trying again...")
            return self.execute(step_by_step_plan, contextual_keywords, project_name)

        return valid_response

Thank me later 👋

Abhishekrajpurohit avatar Apr 04 '24 06:04 Abhishekrajpurohit

Change the code in the file path 'src/agents/researcher/researcher.py' to this code

import json
from typing import List
from jinja2 import Environment, BaseLoader
from typing import Union
from src.llm import LLM
from src.browser.search import BingSearch

PROMPT = open("src/agents/researcher/prompt.jinja2").read().strip()


class Researcher:
    def __init__(self, base_model: str):
        self.bing_search = BingSearch()
        self.llm = LLM(model_id=base_model)

    def render(self, step_by_step_plan: str, contextual_keywords: str) -> str:
        env = Environment(loader=BaseLoader())
        template = env.from_string(PROMPT)
        return template.render(
            step_by_step_plan=step_by_step_plan,
            contextual_keywords=contextual_keywords
        )

    def validate_response(self, response: str) -> Union[dict , bool]:
        response = response.strip().replace("```json", "```")

        if response.startswith("```") and response.endswith("```"):
            response = response[3:-3].strip()
        try:
            response = json.loads(response)
        except Exception as _:
            return False

        response = {k.replace("\\", ""): v for k, v in response.items()}

        if "queries" not in response and "ask_user" not in response:
            return False
        else:
            return {
                "queries": response["queries"],
                "ask_user": response["ask_user"]
            }

    def execute(self, step_by_step_plan: str, contextual_keywords: List[str], project_name: str) -> Union[dict , bool]:
        contextual_keywords_str = ", ".join(map(lambda k: k.capitalize(), contextual_keywords))
        prompt = self.render(step_by_step_plan, contextual_keywords_str)
        
        response = self.llm.inference(prompt, project_name)
        
        valid_response = self.validate_response(response)

        while not valid_response:
            print("Invalid response from the model, trying again...")
            return self.execute(step_by_step_plan, contextual_keywords, project_name)

        return valid_response

Thank me later 👋

@Abhishekrajpurohit Thank you, It worked. You should raise a PR with the changes.

csaicharan avatar Apr 04 '24 07:04 csaicharan