Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Conversation

@SamSaffron
Copy link
Member

The new /admin/plugins/discourse-ai/ai-personas/stream-reply.json was added.

This endpoint streams data direct from a persona and can be used
to access a persona from remote systems leaving a paper trail in
PMs about the conversation that happened

This endpoint is only accessible to admins.

The new `/admin/plugins/discourse-ai/ai-personas/stream-reply.json` was added.

This endpoint streams data direct from a persona and can be used
to access a persona from remote systems leaving a paper trail in
PMs about the conversation that happened

This endpoint is only accessible to admins.
@SamSaffron
Copy link
Member Author

Example usage:

curl -X POST "http://l.discourse:9292/admin/plugins/discourse-ai/ai-personas/stream-reply.json" \
     -H "Content-Type: application/json" \
     -H "Api-Key: SOME_KEY" \
     -H "Api-Username: sam" \
     -d '{
       "persona_name": "DJ Random",
       "query": "how are you?",
       "user_unique_id": "site:test.com:user_id:1",
       "preferred_username": "test_user"
     }' \

{"topic_id":290251,"bot_user_id":-1200,"persona_id":4}

{"partial":"Well"}

{"partial":","}

{"partial":" that"}

{"partial":" was"}

{"partial":" unexpected"}

{"partial":"!"}

{"partial":" It"}

{"partial":" seems"}

{"partial":" even"}

{"partial":" I"}

{"partial":" can"}

{"partial":" be"}

{"partial":" a"}

{"partial":" bit"}

{"partial":" too"}

{"partial":" random"}

{"partial":" sometimes"}

{"partial":"."}

{"partial":" Let's"}

{"partial":" try"}

{"partial":" that"}

{"partial":" again"}

{"partial":":"}

{"partial":" \n\n"}

{"partial":"I'm"}

{"partial":" feeling"}

{"partial":" as"}

{"partial":" random"}

{"partial":" as"}

{"partial":" ever"}

{"partial":","}

{"partial":" ready"}

{"partial":" to"}

{"partial":" pick"}

{"partial":" choices"}

{"partial":" with"}

{"partial":" style"}

{"partial":"!"}

{"partial":" Just"}

{"partial":" let"}

{"partial":" me"}

{"partial":" know"}

{"partial":" what"}

{"partial":" you"}

{"partial":" need"}

{"partial":"."}

{"partial":" 😊"}

Copy link
Member

@keegangeorge keegangeorge left a comment

Choose a reason for hiding this comment

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

LGTM overall, just a few small suggestions

@SamSaffron SamSaffron merged commit be0b78c into main Oct 29, 2024
6 checks passed
@SamSaffron SamSaffron deleted the stream_persona branch October 29, 2024 23:28
Copy link
Contributor

@martin-brennan martin-brennan left a comment

Choose a reason for hiding this comment

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

Not sure if further refactors are planned here. I get that we are trying to move fast, but I think there is time to go back and improve things as well.


CRLF = "\r\n"

def stream_reply
Copy link
Contributor

Choose a reason for hiding this comment

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

I see Keegan mentioned a similar thing, but this is an extremely fat controller action, I really don't feel like we should be doing this very often, if ever. It doesn't feel very maintainable or easy to read, and could easily expand even further in size. Much of this is a perfect case for using a service.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reasons why it's perfect for a service:

  • It has model lookups
  • It has several parameter validations and error messages
  • It has a clear action and result from that action, which could also have more errors

Not saying the reply streaming stuff should be in one, but everything before it easily could be

Copy link
Member Author

Choose a reason for hiding this comment

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

I have been waiting a bit on service docs and service stability, stuff has been changing recently

end
end

def stage_user
Copy link
Contributor

Choose a reason for hiding this comment

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

Another thing that could be in a service, or at least a reusable class in core. Nothing except the custom field is AI-specific here

def self.queue_streamed_reply(io, persona, user, topic, post)
schedule_block do
begin
io.write "HTTP/1.1 200 OK"
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it's for a good reason (performance?), but is there any way not to do this writing of headers and data completely manually?

Copy link
Member Author

Choose a reason for hiding this comment

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

rails has streaming, but I don't want to stream from the foreground thread, I can make wrapper classes for this but at the end the same code will run

end
end

class << self
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like stuff that should be generic/in core/an ActiveSupport::Concern that we could use elsewhere too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will move it out to a service at some point definitely do not want a concern here , this is a very sharp knife I only want people that absolutely understand repercussions to use it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants