Support Docs/Resources for Custom Langchain Code Node

Describe the problem/error/question

Generally speaking, it seems like there is a lack of examples or references on how to utilize the Langchain code node.

For example, I am trying to make a “Retriever as a Tool” workflow. This would use Langchain code node to take in a vector store, manually run the similaritySearch method of the vector store and return the resulting documents. However, when I attempt to use the vector store input, I get an object that seems to have an array of ids, and some other seemingly confusing (to me, I am sure it make sense to you guys) values.

Code Example:
Screenshot 2023-10-17 at 11.10.51 AM

Example Log:
Screenshot 2023-10-17 at 11.12.13 AM

Now, maybe I am missing something. I think perhaps I am supposed to pipe the ids and construct the object from its result. However, there are not a lot of references for me to figure this out. I figured out how the this.getInputConnectionData('ai_vectorStore', 0); method works just from deductive reasoning and trial and error.

So I suppose this is a two part question/request:

  1. Can someone explain to me how to properly get the vector store object in the above example.
  2. Is there any plan on expanding the documentation/references for the LangChain code node?

Information on your n8n setup

  • n8n version: Self Hosted
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): NA
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu

Welcome to the community @MasonGeloso!

Yes, you are totally right. The docs are currently very incomplete. Very sorry about that! I really hope we can improve them very soon.

A few things that should help you to get unblocked.

Under “Code” you can choose between two different types.

  • Execute: This code gets executed when the node receives data via a “main” input, and it can return data via a “main” output. Meaning the inputs/outputs on the left and right sides of the node. So you would have to write code if you for example want to create a custom Root node like Chain or an Agent. The output is the items as JSON.
  • Supply Data: This code gets executed whenever a Root node requests “supporting data” via getInputConnectionData() from a Sub node. That would be something like a model, memory, tool, and so on. The output of such a node is an instance of the appropriate type. For example, a “model” (to be more exact a Language Model) would return for example OpenAI which extends BaseLLM.

Also important to know about the getInputConnectionData(): The output depends on how many inputs are allowed. If “Max Connections” is set to 1 then the output will be directly that instance (for example a language model). If more connections are allowed it would return an an array (for example, an array of those language model instances). This also means that whatever it returns, will probably not be very helpful to output as you learned yourself.

Currently, we have only one example workflow (that will hopefully not be the case for people who look at this post in the future), with one example each (“Execute” + “Supply Data”) . It can be found here.

So what does all of that mean for you?

  1. The code has to be written in “Supply Data”
  2. What that code returns has to be directly the retriever instance (so something that extends BaseRetriever)

I hope that is helpful!

1 Like

Super helpful. Thank you so much for that response.

Sure. fyi the first doc improvements are already online

Hello, first of all I’d like to thank you for your reply, which was very helpful.

I’m having a problem because I’m trying to create a custom LangChain script that would return the document closest to the query as well as its reliability score, unfortunately I’m new to the platform and even with the documentation, I couldn’t figure out how to do it. Here’s my workflow :

thank you in advance for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.