patterns-ai-core / langchainrb

Build LLM-powered applications in Ruby
https://rubydoc.info/gems/langchainrb
MIT License
1.19k stars 159 forks source link

Example for Assistants with Files uploaded into threads #685

Open ausangshukla opened 1 week ago

ausangshukla commented 1 week ago

How can I use an assistant where I upload some documents that the assistant has access to when asked a question? If there is a better forum to ask the question in, pl lmk.

Regards

andreibondarev commented 1 week ago

I assume you'd like the Assistant to perform RAG? If so:

  1. You'd have to manually upload documents to a vectorstore database, let's say Chroma:
    
    chroma = Langchain::Vectorsearch::Chroma.new(url:, index_name:, llm:, api_key: nil)

doc1 = Langchain.root.join("path/to/my.pdf") doc2 = Langchain.root.join("path/to/my.txt") doc3 = Langchain.root.join("path/to/my.docx")

client.add_data(paths: [doc1, doc2, doc3])


2. Instantiate an Assistant and expose it as a tool:
```ruby
vectorsearch_tool = Langchain::Tool::Vectorsearch.new(vectorsearch: chroma)

assistant = Langchain::Assistant.new(
  llm: llm,
  tools: [vectorsearch_tool]

When you run queries now, the Assistant should be retrieving docs from a vectorsearch DB:

assistant.add_message_and_run(content: "...", auto_tool_execution: true)
ausangshukla commented 1 week ago

My use case is different, I dont want to use Vector store and do RAG, I want the whole file to be accessible to GPT-4o. I can do that via the code

client.files.upload(parameters: { file: my_file, purpose: "assistants" })

In the older version of the code, I could pass the following to an assistant.

tools: [
            { type: 'retrieval' }, # Allow access to files attached using file_ids
            { type: 'code_interpreter' } # Allow access to Python code interpreter
],

Now however the new api only takes Langchain::Tool array. How to proceed?

andreibondarev commented 1 week ago

In the older version of the code, I could pass the following to an assistant. tools: [ { type: 'retrieval' }, # Allow access to files attached using file_ids { type: 'code_interpreter' } # Allow access to Python code interpreter ],

Now however the new api only takes Langchain::Tool array. How to proceed?

The Langchain::Assistant class is not a wrapper on top of the OpenAI Assistants API, it's a wrapper on top of any LLM chat completions endpoint that supports Function Calling.

ausangshukla commented 1 week ago

But then how can my assistants access the code interpreter within GPT or the files uploaded to GPT? Sorry for the questions.

andreibondarev commented 1 week ago

@ausangshukla Do you have a hard requirement to use the Python code interpreter or would the RubyCodeInterpreter Tool work for you?

ausangshukla commented 1 week ago

Well the thing is I want to submit an entire XL sheet, as questions of GPT and then get it to give me the ans, which may include using pandas to analyze the docs. So thats the use case, I want to GPT to use its code interpreter, not run code on my machine using ruby.

andreibondarev commented 1 week ago

@ausangshukla You can use the OpenAI Assistants API directly of course. Alternatively -- this gem gives you more control: picking any LLM, any vectorsearch storage, parsing your own data, etc.

Let me think through how this gem could help you out here.

andreibondarev commented 1 week ago

Well the thing is I want to submit an entire XL sheet, as questions of GPT and then get it to give me the ans, which may include using pandas to analyze the docs. So thats the use case, I want to GPT to use its code interpreter, not run code on my machine using ruby.

You could also use https://github.com/ankane/ruby-polars if you'd like to stay in the Ruby ecosystem.