nlkitai / nlux

The π—£π—Όπ˜„π—²π—Ώπ—³π˜‚π—Ή Conversational AI JavaScript Library πŸ’¬ β€”Β UI for any LLM, supporting LangChain / HuggingFace / Vercel AI, and more 🧑 React, Next.js, and plain JavaScript ⭐️
https://docs.nlkit.com/nlux
Other
943 stars 48 forks source link

Set custom headers for adapters #44

Closed jlev closed 3 months ago

jlev commented 3 months ago

I'd starting to production-ize my LLM with NLUX and Langserve. Is there a way to set authorization headers for the adapter? I am using streaming mode, and I'd like to require an auth0 token with each request.

https://github.com/nluxai/nlux/blob/latest/packages/js/langchain/src/langserve/adapter/stream.ts#L22

Should I make a custom adapter, or is this something that could be passed into the ChatAdapterExtras, like it is for NLBridge?

salmenus commented 3 months ago

( I just removed my last comment β€” after looking at the suggested code change )

salmenus commented 3 months ago

Thanks for suggesting this @jlev β€”Β It's an important one, indeed.

Do you want to implement it @jlev ?

In order to enable developers to provide headers to LangServe, here is a suggested way:

API / Usage

The usage should be as per following:

const [customHeaders, setCustomHeaders] = useState<LangServeHeaders>({ key: 'value' });

const adapter = useChatAdapter({
    url: 'https://<Your LangServe Runnable URL>',
    headers: customHeaders
});
  1. New type LangServeHeaders should be added
  2. New attribute headers should be added to LS adapter options
  3. The React hook useChatAdapter should be updated to support the new attribute

Suggested Implementation

Here are few hints to assist in implementing the change

  1. We start by making the change in the JS version packages/js/langchain of the adapter:
  1. We add support for headers option in React Hooks:
  1. We pass the headers to LangChain when available

Give it a try and let me know @jlev β€” I'll keep an eye on Discord to answer any question and assist. I'm assigning this issue to you, since I feel like you'll handle it 😎

salmenus commented 3 months ago

PR updated. New NPM published with code change. Available starting from version 1.0.4 ( @nlux/react & adapters )

Thank you @jlev for contributing πŸ™