OlympiaAI / raix-rails

Ruby AI eXtensions for Rails
MIT License
49 stars 4 forks source link

Challenges getting started #3

Open mghaught opened 3 days ago

mghaught commented 3 days ago

I tried out Raix-rails today in a brand new app to play around with an AI prompt concept I have. For context, this is my first LLM interaction from Ruby code so consider me a newbie with AI interaction. Second, I am planning to only use OpenRouter in my LLM interactions.

I ran into two issues trying this out in the console.

1) I included Raix::ChatCompletion into my model but needed to require 'raix' first. Maybe this is how you intend it to work vs Rails autoloading. If so, it would be nice to update the README to mention that. After I did the require, I ran into an issue that it needed openai. I didn't see it included in the gem dependencies. Adding gem "ruby-openai" to my Gemfile fixed that. Maybe we should include that in the gem dependencies?

2) I followed the first README example of this:

transcript << { user: "What is the meaning of life?" } chat_completion

It gets this error:

undefined method complete' for nil (NoMethodError)

Raix.configuration.openrouter_client.complete(messages, model:, extras: params.compact, stream:) ^^^^^^^^^

My guess is that I'm missing some basic config/setup to work with OpenRouter. I set my OR_ACCESS_TOKEN in my .env file so that seems fine. My account on OpenRouter looks good but I didn't put any credits into it yet. I haven't dug through the code to solve it yet but figured I'd share this as an issue. Thanks!

obie commented 3 days ago

Thank you for submitting this issue. I will look into this asap tonight. On Jul 2, 2024 at 3:02 PM -0600, Marty Haught @.***>, wrote:

I tried out Raix-rails today in a brand new app to play around with an AI prompt concept I have. For context, this is my first LLM interaction from Ruby code so consider me a newbie with AI interaction. Second, I am planning to only use OpenRouter in my LLM interactions. I ran into two issues trying this out in the console.

  1. I included Raix::ChatCompletion into my model but needed to require 'raix' first. Maybe this is how you intend it to work vs Rails autoloading. If so, it would be nice to update the README to mention that. After I did the require, I ran into an issue that it needed openai. I didn't see it included in the gem dependencies. Adding gem "ruby-openai" to my Gemfile fixed that. Maybe we should include that in the gem dependencies?

  2. I followed the first README example of this:

transcript << { user: "What is the meaning of life?" } chat_completion It gets this error: undefined method complete' for nil (NoMethodError) Raix.configuration.openrouter_client.complete(messages, model:, extras: params.compact, stream:) ^^^^^^^^^ My guess is that I'm missing some basic config/setup to work with OpenRouter. I set my OR_ACCESS_TOKEN in my .env file so that seems fine. My account on OpenRouter looks good but I didn't put any credits into it yet. I haven't dug through the code to solve it yet but figured I'd share this as an issue. Thanks! — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

mghaught commented 11 hours ago

Thanks! I'll be working on this again on Monday.

Do you have an example you can share of the config or setup you use in Olympia? I'm assuming that this isn't a bug but a missing bit of setup that is needed.

obie commented 1 hour ago

I whipped up a sample app and this is how you setup the OpenRouter and OpenAI clients. Note that the ENV variables containing API keys are in a .env file that is loaded by the Dotenv library.

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "raix/sampleapp"
require "dotenv"

Dotenv.load

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

Raix.configure do |config|
  config.openrouter_client = OpenRouter::Client.new(access_token: ENV["OR_ACCESS_TOKEN"])
  config.openai_client = OpenAI::Client.new(access_token: ENV["OAI_ACCESS_TOKEN"]) do |f|
    f.request :retry, retry_options
    f.response :logger, ::Logger.new($stdout), { headers: true, bodies: true, errors: true } do |logger|
      logger.filter(/(Bearer) (\S+)/, '\1[REDACTED]')
    end
  end
end

require "irb"
IRB.start(__FILE__)

Once I did that I was able to write a simple completion class like this:

module Raix
  module Sampleapp
    # This class provides the meaning of life functionality.
    class MeaningOfLife
      include Raix::ChatCompletion

      def initialize
        transcript << { user: "What is the meaning of life." }
      end
    end
  end
end
> Raix::Sampleapp::MeaningOfLife.new.chat_completion
=> "The meaning of life is a question that has puzzled philosophers, scientists, and thinkers for centuries. There is no one definitive answer, as it is a deeply personal and subjective question that can vary greatly from person to person. However, here are some possible perspectives and insights that may help shed some light on this question:\n\n1. **Existentialism**: According to existentialist philosophers like Jean-Paul Sartre and Martin Heidegger, the meaning of life is not predetermined or given by external forces. Instead, it is created by individuals through their choices, actions, and experiences. In this view, the meaning of life is subjective and personal, and it is up to each individual to create their own meaning.\n2. **Purpose**: Some people believe that the meaning of life is to find one's purpose or passion. This could be a career, a hobby, a relationship, or a personal goal. When we are doing something we love and are good at, we feel fulfilled and satisfied, which can give our life meaning.\n3. **Happiness**: Another perspective is that the meaning of life is to pursue happiness and well-being. This could involve cultivating positive relationships, achieving personal growth, and finding joy and fulfillment in life's experiences.\n4. **Spirituality**: For many people, the meaning of life is connected to their spiritual beliefs and values. This could involve seeking a deeper connection with a higher power, finding inner peace, or living in accordance with a set of moral principles.\n5. **Evolutionary perspective**: From an evolutionary perspective, the meaning of life could be seen as the continuation of the human species. Our existence is a result of millions of years of evolution, and our purpose is to survive, reproduce, and pass on our genes to future generations.\n6. **Cosmic perspective**: Some people find meaning in life by considering the vastness and complexity of the universe. This could involve appreciating the beauty and wonder of nature, the mysteries of the cosmos, or the interconnectedness of all things.\n7. **Moral values**: Another perspective is that the meaning of life is to live in accordance with moral values such as kindness, compassion, and justice. When we act with integrity and treat others with respect and empathy, we can find meaning and purpose in our lives.\n8. **Personal growth**: Some people believe that the meaning of life is to grow and develop as individuals. This could involve learning new skills, overcoming challenges, and becoming the best version of ourselves.\n9. **Legacy**: Finally, some people find meaning in life by considering the impact they will have on the world after they are gone. This could involve leaving a lasting legacy, making a positive difference in the lives of others, or contributing to the greater good.\n\nUltimately, the meaning of life is a deeply personal and subjective question that can vary greatly from person to person. While these perspectives may provide some insights, it is up to each individual to discover their own meaning and purpose in life."