Skip to content

Error within get_candidate_tasks #5

@Marcus-M1999

Description

@Marcus-M1999

The error shows up as this:
ERROR:root:Failed to get candidate conclusions: string indices must be integers, not 'str'

This happens when the LLM doesn't return strictly a json object and instead a string with text and the json object (as some open source LLMs do with the default prompts). A quick fix I found for this was in the get_initial_conclusion function, adding in something like this to capture things that look like json objects:


    # Regex to find JSON array enclosed in []
    json_match = re.search(r'\[\s*{.*}\s*\]', response, re.DOTALL)

    if json_match:
        json_str = json_match.group()
        try:
            response = json.loads(json_str)
        except json.JSONDecodeError as e:
            print("Failed to parse JSON:", e)
    else:
        print("No JSON array found in text")

The final function would look something like this:


def get_initial_conclusion(
        args,
        model,
        content_identifier,
        prompt_input,
):
    """
    
    This function retrieves initial conclusions from the provided document section
    based on the specified model and language.

    Args:
        model: The model used for processing the content.
        section: The section of the document to be processed.
    Returns:
        A list of validated conclusions, each containing the conclusion text, 
        its relationship (R), and the content identifier.
    """
    if args.modal == "single":
        developer_prompt = atomic_prompt_templates["get_initial_conclusion_for_default_prompt"]
        prompt = f"""
        The document content to be processed is as follows: {prompt_input}
        """
    elif args.modal == "multi":
        current_description = prompt_input['description']
        current_analysis = prompt_input['analysis']
        developer_prompt = atomic_prompt_templates["get_initial_conclusion_for_image_prompt"]
        prompt = f"""
        The following is the description of the image with the caption "{current_description}" in the article titled "{content_identifier}":
        {current_analysis}
        """

    response = run_llm_prompt(model, prompt, developer_prompt, return_json=True)
    # Regex to find JSON array enclosed in []
    json_match = re.search(r'\[\s*{.*}\s*\]', response, re.DOTALL)

    if json_match:
        json_str = json_match.group()
        try:
            response = json.loads(json_str)
        except json.JSONDecodeError as e:
            print("Failed to parse JSON:", e)
    else:
        print("No JSON array found in text")

    validated_conclusions = []
    for item in response:
        conclusion = item['conclusion']
        R = item['R']
        validated_conclusions.append({
            'conclusion': conclusion,
            'R': R,
        })
    return validated_conclusions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions