langchain-ai / langchain

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

Trying to understand the code; general questions #721

Closed abhimanyupallavisudhir closed 1 year ago

abhimanyupallavisudhir commented 1 year ago

I had been planning to write a similar library myself (for encyclopaedic article generation) and was pleasantly surprised when I saw that you guys had beaten me to it.

I'm looking through the code now -- with the intention of contributing code for my specific purposes -- I have a bunch of questions, which I'm documenting here and attempting to answer for myself as I go through them, but help would be appreciated on the unanswered ones!

Function

There seem to be multiple ways to call a chain, e.g. _call, run, apply, predict, generate. What's the difference between them?

OK I see: __call__ (the method Python calls by default when you try to use a Python class as a function) is defined at the Chain level, so all chains have the same implementation -- but it depends on _call, which is defined differently for each subclass.

apply is just __call__ applied pointwise to a list of inputs. run just removes the dict wrapping from the output of __call__.

generate and predict are specifically methods for LLMchains. generate operates on lists and returns an object of type LLMResult, which can contain metadata in addition to the result. predict is the straightforward version.

Conventions for providing examples in prompts -- I recall that Prompt was configured to admit examples as an option, but e.g. the stuff chain has it built-into EXAMPLE_PROMPT ... ?

Style

Any reason why everything is defined as a class variable? It makes sense to me for things like 'input_key and output_key but e.g. StuffDocumentsChain.llm_chain, document_variable_name are only provided type hints and not assigned values at the class level, so aren't they better thought of as instance variables?

Dev Tools & Setting up

I'm having trouble using Poetry on Codespaces. The instructions don't seem to help.

This I figured out -- the virtual environment isn't activated by default. Codespaces has Poetry installed by default, so you just need to run source .venv/bin/activate.

Pylint and Pylance give errors on the existing code -- e.g. import errors (although everything is actually imported, and can be used in runtime).

abhimanyupallavisudhir commented 1 year ago

This was supposed to be in Discussions, my bad