-
Notifications
You must be signed in to change notification settings - Fork 0
[DRAFT][DO NOT MERGE] Add helpers #1
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
Open
arturantonnikau
wants to merge
2
commits into
main
Choose a base branch
from
aa/add-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.configure do |config| | ||
| WebMock.allow_net_connect! | ||
|
|
||
| config.api_endpoint = 'http://localhost:3000' | ||
| config.auth_endpoint = 'http://localhost:3000/users/sign_in' | ||
| config.api_creds = { user: { email: '***email***', password: '***password***' } } | ||
|
|
||
| config.include Rspec::Llama::Helpers | ||
| end | ||
|
|
||
| RSpec.describe 'Llama Rspec flow' do | ||
| let(:model_version_name) { 'Version 1' } | ||
|
|
||
| context 'when we want to fetch resources' do | ||
| before do | ||
| use_model('LLAMA_C', 'Vesion C') | ||
| build_prompt('pt_c', 'Is Minsk the capital of Belarus?') | ||
| build_assertion('1_ass', 'Yes') | ||
| use_prompt('pt_c') | ||
| use_assertion('1_ass') | ||
| end | ||
|
|
||
| it 'should fetch model & model_version' do | ||
| expect(settled_model['name']).to eq('LLAMA_C') | ||
| expect(settled_model_version['build_name']).to eq('Vesion C') | ||
| expect(settled_prompt['name']).to eq('pt_c') | ||
| expect(settled_assertion['name']).to eq('1_ass') | ||
| end | ||
| end | ||
|
|
||
| context 'when we want to create resources(prompt, assertion)' do | ||
| before do | ||
| use_model('LLAMA_C', 'Vesion C') | ||
| build_prompt('pt_s', 'Is Minsk the capital of Belarus?') | ||
| build_assertion('ass_1', 'Yes') | ||
| end | ||
|
|
||
| it 'should create prompt & assertion' do | ||
| expect(settled_prompt['name']).to eq('pt_s') | ||
| expect(settled_assertion['name']).to eq('ass_1') | ||
| end | ||
| end | ||
|
|
||
| context 'when we want to set resources' do | ||
| before do | ||
| use_model('LLAMA', model_version_name) | ||
| build_prompt('pt_2', 'What is the capital of France?') | ||
| build_assertion('ass_2', 'No') | ||
| end | ||
|
|
||
| it 'should set model, prompt, assertion' do | ||
| expect(settled_model['name']).to eq('LLAMA') | ||
| expect(settled_model_version['build_name']).to eq(model_version_name) | ||
| expect(settled_prompt['name']).to eq('pt_2') | ||
| expect(settled_assertion['name']).to eq('ass_2') | ||
| end | ||
| end | ||
|
|
||
| context 'when we want to create resources' do | ||
| before do | ||
| use_model('LLAMA_C', 'Vesion C') | ||
| build_prompt('pt_c', 'What is the capital of France?') | ||
| build_assertion('ass_c', 'Paris') | ||
| end | ||
|
|
||
| it 'should create model, model_version, prompt, assertion' do | ||
| expect(settled_model['name']).to eq('LLAMA_C') | ||
| expect(settled_model_version['build_name']).to eq('Vesion C') | ||
| expect(settled_prompt['name']).to eq('pt_c') | ||
| expect(settled_assertion['name']).to eq('ass_c') | ||
| end | ||
| end | ||
|
|
||
| describe '#execute_test_run' do | ||
| before do | ||
| use_model(model_name, model_version_name) | ||
| use_prompt('pt_2') | ||
| use_assertion('ass_2') | ||
| end | ||
|
|
||
| let(:action) { execute_test_run } | ||
|
|
||
| context 'model version 1' do | ||
| let(:model_name) { 'LLAMA' } | ||
| let(:model_version_name) { 'Version 2' } | ||
| it 'should return a completed status' do | ||
| action | ||
| expect(test_run).to be_successful | ||
| end | ||
| end | ||
|
|
||
| context 'model version 2' do | ||
| let(:model_name) { 'LLAMA' } | ||
| let(:model_version_name) { 'Version 2' } | ||
|
|
||
| it 'should return a completed status' do | ||
| action | ||
| expect(test_run).to be_successful | ||
| end | ||
|
|
||
| context 'create a new prompt & assertion' do | ||
| before do | ||
| build_prompt('pt_2', 'what is the capital of france?') | ||
| build_assertion('ass_2', 'paris') | ||
| end | ||
|
|
||
| it 'should return a completed status' do | ||
| action | ||
| expect(test_run).to be_failed | ||
| end | ||
| end | ||
|
|
||
| context 'with more manual testing' do | ||
| before do | ||
| build_prompt('pt_2', 'what is the capital of france?') | ||
| build_assertion('ass_2', 'paris') | ||
| end | ||
|
|
||
| it 'should return a completed status' do | ||
| action | ||
| expect(settled_test_results.last['status']).to eq('completed') | ||
| expect(settled_assertion_results.first['state']).to eq('failed') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,38 +9,108 @@ def initialize(endpoint, creds, auth_endpoint) | |
| @endpoint = endpoint | ||
| @creds = creds | ||
| @auth_endpoint = auth_endpoint | ||
|
|
||
| set_token | ||
| end | ||
|
|
||
| def authenticate | ||
| uri = URI(auth_endpoint) | ||
| req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') | ||
| req.body = creds.to_json | ||
| api_execute(:post, uri, creds) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
| http.request(req) | ||
| end | ||
| def fetch_all_models | ||
| uri = URI("#{endpoint}/models.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_all_prompts | ||
| uri = URI("#{endpoint}/prompts.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_all_assertions | ||
| uri = URI("#{endpoint}/assertions.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_all_model_versions(model_id) | ||
| uri = URI("#{endpoint}/models/#{model_id}/model_versions.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_assertion_results(test_result_id) | ||
| uri = URI("#{endpoint}/test_results/#{test_result_id}/assertion_results.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_model(id) | ||
| uri = URI("#{endpoint}/models/#{id}.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_test_run(test_run_id) | ||
| uri = URI("#{endpoint}/test_runs/#{test_run_id}.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| response['Authorization'].gsub('Bearer ', '') | ||
| def fetch_test_model_version_run(test_model_version_run_id) | ||
| uri = URI("#{endpoint}/test_model_version_runs/#{test_model_version_run_id}.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def fetch_test_result(test_result_id) | ||
| uri = URI("#{endpoint}/test_results/#{test_result_id}.json") | ||
| api_execute(:get, uri) | ||
| end | ||
|
|
||
| def create_model(**opts) | ||
| uri = URI("#{endpoint}/models.json") | ||
| api_execute(:post, uri, opts) | ||
| end | ||
|
|
||
| def create_model_version(model_id, opts) | ||
| uri = URI("#{endpoint}/models/#{model_id}/model_versions.json") | ||
| api_execute(:post, uri, opts) | ||
| end | ||
|
|
||
| def create_prompt(opts) | ||
| params = { prompt: { value: opts[:value], name: opts[:name] } } | ||
| uri = URI("#{endpoint}/prompts.json") | ||
| api_execute(:post, uri, params) | ||
| end | ||
|
|
||
| def create_assertion(opts) | ||
| uri = URI("#{endpoint}/assertions.json") | ||
| api_execute(:post, uri, opts) | ||
| end | ||
|
|
||
| def execute_test_run(params) | ||
| uri = URI("#{endpoint}/test_runs.json") | ||
| req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') | ||
| req.body = params.to_json | ||
| req['Authorization'] = "Bearer #{token}" | ||
| api_execute(:post, uri, params) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def api_execute(method, uri, params = nil) | ||
| is_auth_req = uri.to_s.include?('sign_in') | ||
|
|
||
| req = Net::HTTP.const_get(method.capitalize).new(uri, 'Content-Type' => 'application/json') | ||
| req['Authorization'] = "Bearer #{token}" unless is_auth_req | ||
| req.body = params.to_json if params | ||
|
|
||
| response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
| http.request(req) | ||
| end | ||
|
|
||
| JSON.parse(response.body) | ||
| is_auth_req ? response : JSON.parse(response.body) | ||
| rescue StandardError => e | ||
| # should be implemented | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def token | ||
| @token ||= authenticate | ||
| @token ||= authenticate['Authorization'].gsub('Bearer ', '') | ||
| end | ||
| alias_method :set_token, :token | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "active_support/all" | ||
|
|
||
| require_relative 'helpers/resource_handler' | ||
| require_relative 'helpers/executor' | ||
| require_relative 'helpers/base' | ||
| require_relative 'helpers/errors' | ||
|
|
||
| module Rspec | ||
| module Llama | ||
| module Helpers | ||
| def execute_test_run(test_id) | ||
| Rspec::Llama.api_client.execute_test_run(test_id) | ||
| end | ||
| include Base | ||
| include ResourceHandler | ||
| include Executor | ||
| include Errors | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend to remove it.