This shows you the differences between two versions of the page.
| wiki:ai:agentic_rag [2025/09/12 19:58] – created ymurugesan | wiki:ai:agentic_rag [2025/09/12 20:01] (current) – ymurugesan | ||
|---|---|---|---|
| Line 195: | Line 195: | ||
| </ | </ | ||
| + | < | ||
| + | agent = ChatCompletionAgent( | ||
| + | service=chat_completion_service, | ||
| + | plugins=[SearchPlugin(search_client=search_client), | ||
| + | name=" | ||
| + | instructions=" | ||
| + | ) | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | |||
| + | |||
| + | async def main(): | ||
| + | thread: ChatHistoryAgentThread | None = None | ||
| + | |||
| + | user_inputs = [ | ||
| + | "Can you share egg price in february 2025?", | ||
| + | "What is the consumer price index for August 2025?", | ||
| + | |||
| + | ] | ||
| + | |||
| + | for user_input in user_inputs: | ||
| + | html_output = ( | ||
| + | f"< | ||
| + | f"< | ||
| + | f"< | ||
| + | ) | ||
| + | |||
| + | agent_name = None | ||
| + | full_response: | ||
| + | function_calls: | ||
| + | |||
| + | # Buffer to reconstruct streaming function call | ||
| + | current_function_name = None | ||
| + | argument_buffer = "" | ||
| + | |||
| + | async for response in agent.invoke_stream( | ||
| + | messages=user_input, | ||
| + | thread=thread, | ||
| + | ): | ||
| + | thread = response.thread | ||
| + | agent_name = response.name | ||
| + | content_items = list(response.items) | ||
| + | |||
| + | for item in content_items: | ||
| + | if isinstance(item, | ||
| + | if item.function_name: | ||
| + | current_function_name = item.function_name | ||
| + | |||
| + | # Accumulate arguments (streamed in chunks) | ||
| + | if isinstance(item.arguments, | ||
| + | argument_buffer += item.arguments | ||
| + | elif isinstance(item, | ||
| + | # Finalize any pending function call before showing result | ||
| + | if current_function_name: | ||
| + | formatted_args = argument_buffer.strip() | ||
| + | try: | ||
| + | parsed_args = json.loads(formatted_args) | ||
| + | formatted_args = json.dumps(parsed_args) | ||
| + | except Exception: | ||
| + | pass # leave as raw string | ||
| + | |||
| + | function_calls.append(f" | ||
| + | current_function_name = None | ||
| + | argument_buffer = "" | ||
| + | |||
| + | function_calls.append(f" | ||
| + | elif isinstance(item, | ||
| + | full_response.append(item.text) | ||
| + | |||
| + | if function_calls: | ||
| + | html_output += ( | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | " | ||
| + | f" | ||
| + | "</ | ||
| + | ) | ||
| + | |||
| + | html_output += ( | ||
| + | "< | ||
| + | f"< | ||
| + | f"< | ||
| + | ) | ||
| + | |||
| + | display(HTML(html_output)) | ||
| + | |||
| + | await main() | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | Testing | ||
| + | |||
| + | < | ||
| + | |||
| + | async def interactive_conversation(): | ||
| + | """ | ||
| + | thread: ChatHistoryAgentThread | None = None | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | | ||
| + | while True: | ||
| + | try: | ||
| + | # Get user input | ||
| + | user_input = input(" | ||
| + | | ||
| + | if user_input.lower() in [' | ||
| + | print(" | ||
| + | break | ||
| + | | ||
| + | if not user_input: | ||
| + | continue | ||
| + | | ||
| + | print(" | ||
| + | | ||
| + | # Track function calls and responses | ||
| + | function_calls = [] | ||
| + | full_response = [] | ||
| + | | ||
| + | # Buffer for function call reconstruction | ||
| + | current_function_name = None | ||
| + | argument_buffer = "" | ||
| + | | ||
| + | # Stream the agent response | ||
| + | async for response in agent.invoke_stream( | ||
| + | messages=user_input, | ||
| + | thread=thread, | ||
| + | ): | ||
| + | thread = response.thread | ||
| + | content_items = list(response.items) | ||
| + | | ||
| + | for item in content_items: | ||
| + | if isinstance(item, | ||
| + | if item.function_name: | ||
| + | current_function_name = item.function_name | ||
| + | | ||
| + | # Accumulate arguments (streamed in chunks) | ||
| + | if isinstance(item.arguments, | ||
| + | argument_buffer += item.arguments | ||
| + | | ||
| + | elif isinstance(item, | ||
| + | # Finalize any pending function call | ||
| + | if current_function_name: | ||
| + | formatted_args = argument_buffer.strip() | ||
| + | try: | ||
| + | parsed_args = json.loads(formatted_args) | ||
| + | formatted_args = json.dumps(parsed_args, | ||
| + | except Exception: | ||
| + | pass | ||
| + | | ||
| + | function_calls.append({ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }) | ||
| + | | ||
| + | current_function_name = None | ||
| + | argument_buffer = "" | ||
| + | | ||
| + | elif isinstance(item, | ||
| + | # Print response as it streams | ||
| + | print(item.text, | ||
| + | full_response.append(item.text) | ||
| + | | ||
| + | print() | ||
| + | | ||
| + | # Optionally show function calls in verbose mode | ||
| + | if function_calls and False: | ||
| + | print(" | ||
| + | for call in function_calls: | ||
| + | print(f" | ||
| + | print(f" | ||
| + | | ||
| + | except KeyboardInterrupt: | ||
| + | print(" | ||
| + | break | ||
| + | except Exception as e: | ||
| + | print(f" | ||
| + | print(" | ||
| + | |||
| + | # Run the interactive conversation | ||
| + | await interactive_conversation() | ||
| </ | </ | ||