Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec 'integration' tests for optional use by contributors and ci #145

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ Langchain.logger.level = :info
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`

## Community
Join us in the [Ruby AI Builders](https://discord.gg/SBmjAnKT) Discord community in #langchainrb
Expand Down
1 change: 1 addition & 0 deletions examples/store_and_query_with_weaviate.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "langchain"
require "weaviate"

# gem install weaviate-ruby
# or add `gem "weaviate-ruby"` to your Gemfile
Expand Down
27 changes: 27 additions & 0 deletions spec/integration/chain_of_thought_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way that people do this is indexing into a vectorsearch DB all of the variations of possible acceptable answers and then run a vector search agains them to see if they "match".

end
end
38 changes: 38 additions & 0 deletions spec/integration/weaviate_with_openai_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably just filter out all integration tests by default is ENV["CI"] == true


config.expect_with :rspec do |c|
c.syntax = :expect
end
Expand Down