Interacting with different LLMs
- Generally we might need to have our agent/rag work irrespective on LLM without huge changes i.e. we are abstracting the LLM interactions.
-
Langchain and other frameworks (LLAMA Index) abstract the LLM interactions.
-
Lets try to interact with openAI using langchain and then use the exact same code to interact with other model with minor changes.
Setup langchain
- Navigate to the python code created in last session or you can create a new python project
- Add a package langchain
uv add langchain
uv add langchain_openai

- Refer Here for sample code to interact with two different llms
Langchain Terminology
- Langchain provides two simple interfaces to interact with LLM API Providers
- Chat Models
- Text Models
- As of now it is recommened to use ChatModels. Text Models have very basic features.
- Most of Langchain (Lang-framework) objects are derived from a interface called as Runnable Interface
- Runnable interface has 4 key methods
- invoke/ainvoke: Transforms a single input into an output.
- batch/abatch: Efficiently transforms multiple inputs into outputs.
- stream/astream: Streams output from a single input as it’s produced.
- astream_log: Streams output and selected intermediate results from an input.
- While interacting with LLMS we have Messages. Some of the popular messages are
- SystemMessage: System
- AI Message : Response generated by LLM
- HumanMessage: Question/prompt
