@@ -4,7 +4,7 @@ Copyright 2024 The Dapr Authors
44you may not use this file except in compliance with the License.
55You may obtain a copy of the License at
66
7- http://www.apache.org/licenses/LICENSE-2.0
7+ http://www.apache.org/licenses/LICENSE-2.0
88
99Unless required by applicable law or agreed to in writing, software
1010distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,55 +13,167 @@ You may obtain a copy of the License at
1313limitations under the License.
1414*/
1515
16- using System . Net . Http ;
16+ using System . Net . Http . Json ;
1717using System . Text . Json ;
18- using System . Text ;
1918
20- class Program
21- {
22- private const string ConversationComponentName = "echo" ;
19+ // const string conversationComponentName = "echo";
20+ const string conversationText = "What is dapr?" ;
21+ const string toolCallInput = "What is the weather like in San Francisco in celsius?" ;
22+
23+ //
24+ // Setup
25+
26+ var httpClient = new HttpClient ( ) ;
2327
24- static async Task Main ( string [ ] args )
28+ //
29+ // Simple Conversation
30+
31+ var conversationRequestBody = JsonSerializer . Deserialize < Dictionary < string , object ? > > ( """
2532 {
26- var daprHost = Environment . GetEnvironmentVariable ( "DAPR_HOST" ) ?? "http://localhost" ;
27- var daprHttpPort = Environment . GetEnvironmentVariable ( "DAPR_HTTP_PORT" ) ?? "3500" ;
28-
29- var client = new HttpClient
30- {
31- Timeout = TimeSpan . FromSeconds ( 15 )
32- } ;
33-
34- var inputBody = new
35- {
36- name = "echo" ,
37- inputs = new [ ] { new { content = "What is dapr?" } } ,
38- parameters = new { } ,
39- metadata = new { }
40- } ;
41-
42- var daprUrl = $ "{ daprHost } :{ daprHttpPort } /v1.0-alpha1/conversation/{ ConversationComponentName } /converse";
43-
44- try
45- {
46- var content = new StringContent ( JsonSerializer . Serialize ( inputBody ) , Encoding . UTF8 , "application/json" ) ;
47-
48- // Send a request to the echo mock LLM component
49- var response = await client . PostAsync ( daprUrl , content ) ;
50- response . EnsureSuccessStatusCode ( ) ;
51-
52- Console . WriteLine ( "Input sent: " + inputBody . inputs [ 0 ] . content ) ;
53-
54- var responseBody = await response . Content . ReadAsStringAsync ( ) ;
55-
56- // Parse the response
57- var data = JsonSerializer . Deserialize < Dictionary < string , List < Dictionary < string , string > > > > ( responseBody ) ;
58- var result = data ? [ "outputs" ] ? [ 0 ] ? [ "result" ] ;
59-
60- Console . WriteLine ( "Output response: " + result ) ;
61- }
62- catch ( Exception ex )
63- {
64- Console . WriteLine ( "Error: " + ex . Message ) ;
65- }
33+ "name": "echo",
34+ "inputs": [{
35+ "messages": [{
36+ "of_user": {
37+ "content": [{
38+ "text": "What is dapr?"
39+ }]
40+ }
41+ }]
42+ }],
43+ "parameters": {},
44+ "metadata": {}
6645 }
67- }
46+ """ ) ;
47+
48+ var conversationResponse = await httpClient . PostAsJsonAsync ( "http://localhost:3500/v1.0-alpha2/conversation/echo/converse" , conversationRequestBody ) ;
49+ var conversationResult = await conversationResponse . Content . ReadFromJsonAsync < JsonElement > ( ) ;
50+
51+ var conversationContent = conversationResult
52+ . GetProperty ( "outputs" )
53+ . EnumerateArray ( )
54+ . First ( )
55+ . GetProperty ( "choices" )
56+ . EnumerateArray ( )
57+ . First ( )
58+ . GetProperty ( "message" )
59+ . GetProperty ( "content" )
60+ . GetString ( ) ;
61+
62+ Console . WriteLine ( $ "Conversation input sent: { conversationText } ") ;
63+ Console . WriteLine ( $ "Output response: { conversationContent } ") ;
64+
65+ //
66+ // Tool Calling
67+
68+ var toolCallRequestBody = JsonSerializer . Deserialize < Dictionary < string , object ? > > ( """
69+ {
70+ "name": "demo",
71+ "inputs": [
72+ {
73+ "messages": [
74+ {
75+ "of_user": {
76+ "content": [
77+ {
78+ "text": "What is the weather like in San Francisco in celsius?"
79+ }
80+ ]
81+ }
82+ }
83+ ],
84+ "scrubPII": false
85+ }
86+ ],
87+ "parameters": {
88+ "max_tokens": {
89+ "@type": "type.googleapis.com/google.protobuf.Int64Value",
90+ "value": "100"
91+ },
92+ "model": {
93+ "@type": "type.googleapis.com/google.protobuf.StringValue",
94+ "value": "claude-3-5-sonnet-20240620"
95+ }
96+ },
97+ "metadata": {
98+ "api_key": "test-key",
99+ "version": "1.0"
100+ },
101+ "scrubPii": false,
102+ "temperature": 0.7,
103+ "tools": [
104+ {
105+ "function": {
106+ "name": "get_weather",
107+ "description": "Get the current weather for a location",
108+ "parameters": {
109+ "type": "object",
110+ "properties": {
111+ "location": {
112+ "type": "string",
113+ "description": "The city and state, e.g. San Francisco, CA"
114+ },
115+ "unit": {
116+ "type": "string",
117+ "enum": [
118+ "celsius",
119+ "fahrenheit"
120+ ],
121+ "description": "The temperature unit to use"
122+ }
123+ },
124+ "required": [
125+ "location"
126+ ]
127+ }
128+ }
129+ }
130+ ],
131+ "toolChoice": "auto"
132+ }
133+ """ ) ;
134+
135+ var toolCallingResponse = await httpClient . PostAsJsonAsync ( "http://localhost:3500/v1.0-alpha2/conversation/echo/converse" , toolCallRequestBody ) ;
136+ var toolCallingResult = await toolCallingResponse . Content . ReadFromJsonAsync < JsonElement > ( ) ;
137+
138+ var toolCallingContent = toolCallingResult
139+ . GetProperty ( "outputs" )
140+ . EnumerateArray ( )
141+ . First ( )
142+ . GetProperty ( "choices" )
143+ . EnumerateArray ( )
144+ . First ( )
145+ . GetProperty ( "message" )
146+ . GetProperty ( "content" ) ;
147+
148+ var functionCalled = toolCallingResult
149+ . GetProperty ( "outputs" )
150+ . EnumerateArray ( )
151+ . First ( )
152+ . GetProperty ( "choices" )
153+ . EnumerateArray ( )
154+ . First ( )
155+ . GetProperty ( "message" )
156+ . GetProperty ( "toolCalls" )
157+ . EnumerateArray ( )
158+ . First ( )
159+ . GetProperty ( "function" ) ;
160+
161+ var functionName = functionCalled . GetProperty ( "name" ) . GetString ( ) ;
162+ var functionArguments = functionCalled . GetProperty ( "arguments" ) . GetString ( ) ;
163+
164+ var toolCallJson = JsonSerializer . Serialize ( new
165+ {
166+ id = 0 ,
167+ function = new
168+ {
169+ name = functionName ,
170+ arguments = functionArguments ,
171+ } ,
172+ } ) ;
173+
174+ Console . WriteLine ( $ "Tool calling input sent: { toolCallInput } ") ;
175+ Console . WriteLine ( $ "Output message: { toolCallingContent } ") ;
176+ Console . WriteLine ( "Tool calls detected:" ) ;
177+ Console . WriteLine ( $ "Tool call: { toolCallJson } ") ;
178+ Console . WriteLine ( $ "Function name: { functionName } ") ;
179+ Console . WriteLine ( $ "Function arguments: { functionArguments } ") ;
0 commit comments