-
Notifications
You must be signed in to change notification settings - Fork 2
Makes ap chat prettier
#2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f9388f
Add custom amazing_print formatting for AI::Chat
raghubetina e541693
Add amazing_print support for AI::Response
raghubetina 88ccca5
Improve amazing_print formatting for AI objects
raghubetina 4247e43
Add demo2/ to .gitignore
raghubetina 652cf88
Add pretty_print support for Ruby's pp library
raghubetina 81ac143
Merge main
jelaniwoods 6e63430
Resolve inconsistencies
jelaniwoods 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,9 @@ vendor/bundle/ | |
| amazing_print/ | ||
| openai-ruby/ | ||
|
|
||
| # Temp demo scripts | ||
| demo2/ | ||
|
|
||
| # OS files | ||
| .DS_Store | ||
|
|
||
|
|
||
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
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 +1,8 @@ | ||
| require_relative "ai/chat" | ||
|
|
||
| # Load amazing_print extension if amazing_print is available | ||
| begin | ||
| require_relative "ai/amazing_print" | ||
| rescue LoadError | ||
| # amazing_print not available, skip custom formatting | ||
| 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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| require "amazing_print" | ||
|
|
||
| module AmazingPrint | ||
| module AI | ||
| def self.included(base) | ||
| base.send :alias_method, :cast_without_ai, :cast | ||
| base.send :alias_method, :cast, :cast_with_ai | ||
| end | ||
|
|
||
| def cast_with_ai(object, type) | ||
| case object | ||
| when ::AI::Chat | ||
| :ai_object | ||
| else | ||
| cast_without_ai(object, type) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def awesome_ai_object(object) | ||
| case object | ||
| when ::AI::Chat | ||
| format_ai_chat(object) | ||
| else | ||
| awesome_object(object) | ||
| end | ||
| end | ||
|
|
||
| def format_ai_chat(chat) | ||
| vars = [] | ||
|
|
||
| # Format messages with truncation | ||
| if chat.instance_variable_defined?(:@messages) | ||
| messages = chat.instance_variable_get(:@messages).map do |msg| | ||
| truncated_msg = msg.dup | ||
| if msg[:content].is_a?(String) && msg[:content].length > 80 | ||
| truncated_msg[:content] = msg[:content][0..77] + "..." | ||
| end | ||
| truncated_msg | ||
| end | ||
| vars << ["@messages", messages] | ||
| end | ||
|
|
||
| # Add other variables (except sensitive ones) | ||
| skip_vars = [:@api_key, :@client, :@messages] | ||
| chat.instance_variables.sort.each do |var| | ||
| next if skip_vars.include?(var) | ||
| value = chat.instance_variable_get(var) | ||
| vars << [var.to_s, value] unless value.nil? | ||
| end | ||
|
|
||
| format_object(chat, vars) | ||
| end | ||
|
|
||
| def format_object(object, vars) | ||
| data = vars.map do |(name, value)| | ||
| name = colorize(name, :variable) unless @options[:plain] | ||
| "#{name}: #{inspector.awesome(value)}" | ||
| end | ||
|
|
||
| if @options[:multiline] | ||
| "#<#{object.class}\n#{data.map { |line| " #{line}" }.join("\n")}\n>" | ||
| else | ||
| "#<#{object.class} #{data.join(', ')}>" | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| AmazingPrint::Formatter.send(:include, AmazingPrint::AI) |
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
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.
For cleaner formatting, consider filtering out sensitive instance variables (e.g. using reject) and using q.seplist to automatically handle comma separation instead of manual iteration with each index.