Let's go in and see a little more
than what we did in the Hello World,
because the Hello World was a quick and
dirty introduction.
Let's go a little deeper this time, look
at normal calls and look at streaming calls.
So here's some similar code.
It's called a normal versus streaming response in
section four in the sample repo, and we
just make our client as before.
And then we make our chat client, and
we can create our agent.
The agent actually have further information we can
give here, like instructions, name, description, and tools,
and so on.
I will add a bonus video to this
section, going through all the different things, but
throughout the system, we will learn about instructions
and tools and all the common things, but
I will have a section that will go
to every single one of them.
Beyond that, when we have the agent, we
can just get some information about it, like
instructions and stuff, and we have this run
async that we used.
And as you can see, the run async
can have a lot of different overloads, which
we'll also cover as we go along.
There's also a run async with some brackets
here.
This is for being able to do structured
outputs, which we'll save for a dedicated video
on that.
And then there's streaming, which we'll just see
in one second.
So if we do this and let the
system run, we can ask our question and
we can get our response back.
And if we do, we, of course, get
the response, but this response is, as we
see it right here, it's just the two
string of this agent response.
Behind the scene, the agent response is a
much bigger object that have, for example, the
ID of the agent we use, when it
was created, the response ID back uses, as
we saw in that video, and a bunch
more that we will cover later on.
So it's first when I press that we
actually get the response back here.
We can also do streaming.
And if we do streaming, we say run
streaming async, and then I'm asking, in this
case, how to make soup.
The reason why I'm doing that instead of
the capital of France is because it gives
us the option to better see that it's
actually streaming back.
So if I press F5 here and go
to after, look that it's coming back word
by word here.
And the way it does that is we
go and see we get an update.
And the first update is actually not even
any of the text.
It's just, hey, I'm the agent, and it
was created at this point.
So it's not only the text streaming back,
it's the entire request response back.
So second update, we now get that we
have an assistant that is about to give
us some text back.
So we are called the user.
The AI is called the assistant.
And it's now identified that the assistant will
say something to us, but it hadn't done
it.
And now comes the first word in everything,
making.
So you can see it's wrote down making
here.
And this will continue for almost 400 updates
in this case because there's so much to
write.
So you can see it's writing each word.
If I let it run, it will write
all the words.
This is a cool way of doing things,
but it has the drawback that whenever we
do these updates, we can't really get users
back.
It's not like when we have the first
three words that we can say how many
tokens was used for making those three words.
That's only the response object that we get
from normal that can give such things back,
and all the other things that might be
in a call that we are interested in.
So for that reason, the agent framework team
have made that you can store all these
updates.
So if we go in here, it has
the same question.
We will then now store the first update,
the second update, and so on.
And if we let it run, you can
see there's 361 updates.
And if we open it, it takes a
little while.
You can actually see the entire sentence coming
back in an object by object update here.
And if we want to take all these
updates and turn into one of these responses
up here, we have handy methods on the
object extension method that say to agent response.
And if we do that, we actually get
our response back.
And the response is, of course, the entire
thing as if we had run the runAsync
instead.
So it looks exactly the same where we
didn't get the entire text.
Of course, we haven't have written that to
the user.
We don't really care too much here, but
we're interested in the input tokens and the
output tokens and whatever else is in here
as we go along.
So in this case, we're just getting the
output tokens.
So if you want to use runAsync or
streaming, streaming is mostly used only for chatbots
because whenever we want to have it behind
the scenes, it's just extra code that nobody
will see that it was streaming back to
us anyway.
So it's only when I do chatbots that
I actually use the streaming part.
95% of the time, I end up
just using runAsync for the work I do
because I rarely do chatbots.
I more do behind the scenes with AI.
But that's everything there is to normal versus
streaming.