diff --git a/README.md b/README.md index 773b6d4b2..f6805cfe9 100644 --- a/README.md +++ b/README.md @@ -513,6 +513,7 @@ Langchain.logger.level = :debug 3. `bundle exec rake` to ensure that the tests pass and to run standardrb 4. `bin/console` to load the gem in a REPL session. Feel free to add your own instances of LLMs, Tools, Agents, etc. and experiment with them. 5. Optionally, install lefthook git hooks for pre-commit to auto lint: `gem install lefthook && lefthook install -f` +6. Optionally, run tests for the components you will be working with using real KEYS and URLs. For example: `INTEGRATION_TESTS_ENABLED=true bundle exec rspec spec/integration/chain_of_thought_integration_spec.rb` ## Discord Join us in the [Langchain.rb](https://discord.gg/WDARp7J2n8) Discord server. diff --git a/spec/integration/chain_of_thought_integration_spec.rb b/spec/integration/chain_of_thought_integration_spec.rb new file mode 100644 index 000000000..7e470f9f2 --- /dev/null +++ b/spec/integration/chain_of_thought_integration_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# execute with command: +# INTEGRATION_TESTS_ENABLED=true bundle exec rspec spec/integration/chain_of_thought_integration_spec.rb +# +# requires the following environment variables: +# SERPAPI_API_KEY +# OPENAI_API_KEY + +RSpec.describe "Chain of Thought integration with other components", type: :integration do + it "Should run with search and calculator" do + question = "How many full soccer fields would be needed to cover the distance between NYC and DC in a straight line?" + + search_tool = Langchain::Tool::SerpApi.new(api_key: ENV["SERPAPI_API_KEY"]) + calculator_tool = Langchain::Tool::Calculator.new + + openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) + + agent = Langchain::Agent::ChainOfThoughtAgent.new( + llm: openai, + tools: [search_tool, calculator_tool] + ) + result = agent.run(question: question) + + expect(result).to include("distance between NYC and DC") + end +end diff --git a/spec/integration/weaviate_with_openai_integration_spec.rb b/spec/integration/weaviate_with_openai_integration_spec.rb new file mode 100644 index 000000000..9f761c596 --- /dev/null +++ b/spec/integration/weaviate_with_openai_integration_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +# execute with command: +# INTEGRATION_TESTS_ENABLED=true bundle exec rspec spec/integration/weaviate_with_openai_integration_spec.rb +# +# requires the following environment variables: +# OPENAI_API_KEY +# WEAVIATE_URL +# WEAVIATE_API_KEY + +RSpec.describe "Weaviate with OpenAI", type: :integration do + it "Should run as expected" do + weaviate = Langchain::Vectorsearch::Weaviate.new( + url: ENV["WEAVIATE_URL"], + api_key: ENV["WEAVIATE_API_KEY"], + index_name: "Recipes", + llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) + ) + + weaviate.create_default_schema + + recipes = [ + "Preheat oven to 400 degrees F (200 degrees C). Cut the top off the head of garlic. Arrange the garlic, carrots, celery, onion, pepper, and tomato on a large baking sheet in a single layer. Drizzle the olive oil over the vegetables; season with salt and pepper. Roast the vegetables in the preheated oven, turning every 20 minutes, until tender and browned, about 1 hour. Combine the water, thyme, parsley, and bay leaves in a large stock pot over medium-high heat. Squeeze the head of garlic into the stock pot, and discard the outer husk. Place the carrots, celery, onion, pepper, and tomato in the stock pot. Bring the water to a boil; reduce heat to low and simmer for 1 1/2 hours; strain and cool.", + "Heat oven to 190C/fan 170C/gas 5. Heat 1 tbsp oil and the butter in a frying pan, then add the onion and fry for 5 mins until softened. Cool slightly. Tip the sausagemeat, lemon zest, breadcrumbs, apricots, chestnuts and thyme into a bowl. Add the onion and cranberries, and mix everything together with your hands, adding plenty of pepper and a little salt. Cut each chicken breast into three fillets lengthwise and season all over with salt and pepper. Heat the remaining oil in the frying pan, and fry the chicken fillets quickly until browned, about 6-8 mins. Roll out two-thirds of the pastry to line a 20-23cm springform or deep loose-based tart tin. Press in half the sausage mix and spread to level. Then add the chicken pieces in one layer and cover with the rest of the sausage. Press down lightly. Roll out the remaining pastry. Brush the edges of the pastry with beaten egg and cover with the pastry lid. Pinch the edges to seal, then trim. Brush the top of the pie with egg, then roll out the trimmings to make holly leaf shapes and berries. Decorate the pie and brush again with egg. Set the tin on a baking sheet and bake for 50-60 mins, then cool in the tin for 15 mins. Remove and leave to cool completely. Serve with a winter salad and pickles." + ] + + weaviate.add_texts( + texts: recipes + ) + + result = weaviate.similarity_search( + query: "chicken", + k: 1 + ).to_s + + expect(result).to include("Heat oven to 190C") + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6c1c53729..4fba1c2d4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -17,6 +17,9 @@ # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! + # Only run integration tests when enabled + config.filter_run_excluding type: :integration unless ENV["INTEGRATION_TESTS_ENABLED"] + config.expect_with :rspec do |c| c.syntax = :expect end