langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.42k stars 14.79k forks source link

value is not a valid dict (type=type_error.dict) #12176

Closed jonty-esterhuizen closed 10 months ago

jonty-esterhuizen commented 11 months ago

Issue you'd like to raise.

when i try and run this script

class WebsiteSummary:
    def _SummorizeWebsite(objective, websiteContent):
        my_gpt_instance = MyGPT4ALL()

        textSplitter = RecursiveCharacterTextSplitter(
            separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500
        )

        docs = textSplitter.create_documents([websiteContent])
        print(docs)
        mapPrompt = """"
        write a summary of the following {objective}: 
        {text}
        SUMMARY: 
        """
        map_prompt_template = PromptTemplate(
            template=mapPrompt, input_variables=["text", "objective"]
        ) 
        chain = load_summarize_chain(llm=my_gpt_instance,
                                     chain_type="map_reduce",                                     
                                     verbose=True,
                                     map_prompt=objective,
                                     combine_prompt=map_prompt_template)

        output = summary_Chain.run(input_documents=docs, objective=objective)
        return output

i get this issue Exception has occurred: ValidationError 1 validation error for LLMChain prompt value is not a valid dict (type=type_error.dict) File "E:\StormboxBrain\langchain\websiteSummary.py", line 27, in _SummorizeWebsite chain = load_summarize_chain(llm=my_gpt_instance, File "E:\StormboxBrain\langchain\app.py", line 12, in summary = websiteSummary._SummorizeWebsite(objective, data) pydantic.error_wrappers.ValidationError: 1 validation error for LLMChain

my model does load and can create prompt responses mistral-7b-openorca.Q4_0.gguf

this is mygpt4all class

import os
from pydantic import Field
from typing import List, Mapping, Optional, Any
from langchain.llms.base import LLM
from gpt4all import GPT4All, pyllmodel

class MyGPT4ALL(LLM):
    """
    A custom LLM class that integrates gpt4all models

    Arguments:

    model_folder_path: (str) Folder path where the model lies
    model_name: (str) The name of the model to use (<model name>.gguf)

    backend: (str) The backend of the model (Supported backends: llama/gptj)
    n_threads: (str) The number of threads to use
    n_predict: (str) The maximum numbers of tokens to generate
    temp: (str) Temperature to use for sampling
    top_p: (float) The top-p value to use for sampling
    top_k: (float) The top k values use for sampling
    n_batch: (int) Batch size for prompt processing
    repeat_last_n: (int) Last n number of tokens to penalize
    repeat_penalty: (float) The penalty to apply repeated tokens

    """

    model_folder_path: str = "./models/"
    model_name: str = "mistral-7b-openorca.Q4_0.gguf"

    backend: Optional[str] = "llama"
    temp: Optional[float] = 0.7
    top_p: Optional[float] = 0.1
    top_k: Optional[int] = 40
    n_batch: Optional[int] = 8
    n_threads: Optional[int] = 4
    n_predict: Optional[int] = 256
    max_tokens: Optional[int] = 200
    repeat_last_n: Optional[int] = 64
    repeat_penalty: Optional[float] = 1.18

    # initialize the model
    gpt4_model_instance: Any = None

    def __init__(self, **kwargs):
        super(MyGPT4ALL, self).__init__()
        print("init " + self.model_folder_path + self.model_name)
        self.gpt4_model_instance = GPT4All(
            model_name=self.model_name,
            model_path=self.model_folder_path,
            device="cpu",
            verbose=True,
        )
        print("initiolized " + self.model_folder_path + self.model_name)

    @property
    def _get_model_default_parameters(self):
        print("get default params")
        return {
            "max_tokens": self.max_tokens,
            "n_predict": self.n_predict,
            "top_k": self.top_k,
            "top_p": self.top_p,
            "temp": self.temp,
            "n_batch": self.n_batch,
            "repeat_penalty": self.repeat_penalty,
            "repeat_last_n": self.repeat_last_n,
        }

    @property
    def _identifying_params(self) -> Mapping[str, Any]:
        print(" Get all the identifying parameters")
        return {
            "model_name": self.model_name,
            "model_path": self.model_folder_path,
            "model_parameters": self._get_model_default_parameters,
        }

    @property
    def _llm_type(self) -> str:
        return "llama"

    def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
        """
        Args:
            prompt: The prompt to pass into the model.
            stop: A list of strings to stop generation when encountered

        Returns:
            The string generated by the model
        """

        params = {**self._get_model_default_parameters, **kwargs}
        print("params:")
        for key, value in params.items():
            print(f"{key}: {value}")
        print("params " + params.values())
        return "initiolized"

Suggestion:

is there any documentation on how to use another llm as base and not gpt

dosubot[bot] commented 11 months ago

🤖

Hello,

The error you're encountering is likely due to the map_prompt argument in the load_summarize_chain function. The map_prompt argument should be an instance of PromptTemplate, but in your code, it seems like you're passing a string (objective).

Here's how you can fix it:

map_prompt_template = PromptTemplate(
    template=mapPrompt, input_variables=["text", "objective"]
) 

chain = load_summarize_chain(llm=my_gpt_instance,
                             chain_type="map_reduce",                                     
                             verbose=True,
                             map_prompt=map_prompt_template,
                             combine_prompt=map_prompt_template)

In the above code, I've replaced map_prompt=objective with map_prompt=map_prompt_template.

As for your question about using a different language model as a base instead of GPT, the LangChain framework is designed to work with any language model that is compatible with its interfaces. You can use any language model that is an instance of BaseLanguageModel. In your case, you have correctly created a custom LLM class MyGPT4ALL that extends LLM.

However, please note that the _call method in your MyGPT4ALL class currently returns a hardcoded string "initiolized". This method should be implemented to call your custom language model and return the generated text.

I hope this helps! Let me know if you have any other questions.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.