User Tools

Site Tools


wiki:ai:agentic_rag

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:ai:agentic_rag [2025/09/12 19:58] – created ymurugesanwiki:ai:agentic_rag [2025/09/12 20:01] (current) ymurugesan
Line 195: Line 195:
 </code> </code>
  
 +<code>
  
 +agent = ChatCompletionAgent(
 +    service=chat_completion_service,
 +    plugins=[SearchPlugin(search_client=search_client), EggPriceInfoPlugin()],
 +    name="PriceAgent",
 +    instructions="Answer price queries using the provided tools and context. If context is provided, do not say 'I have no context for that'",
 +)
  
 +</code>
 +
 +
 +<code>
 +
 +
 +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"<div style='margin-bottom:10px'>"
 +            f"<div style='font-weight:bold'>User:</div>"
 +            f"<div style='margin-left:20px'>{user_input}</div></div>"
 +        )
 +
 +        agent_name = None
 +        full_response: list[str] = []
 +        function_calls: list[str] = []
 +
 +        # 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, FunctionCallContent):
 +                    if item.function_name:
 +                        current_function_name = item.function_name
 +
 +                    # Accumulate arguments (streamed in chunks)
 +                    if isinstance(item.arguments, str):
 +                        argument_buffer += item.arguments
 +                elif isinstance(item, FunctionResultContent):
 +                    # 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"Calling function: {current_function_name}({formatted_args})")
 +                        current_function_name = None
 +                        argument_buffer = ""
 +
 +                    function_calls.append(f"\nFunction Result:\n\n{item.result}")
 +                elif isinstance(item, StreamingTextContent) and item.text:
 +                    full_response.append(item.text)
 +
 +        if function_calls:
 +            html_output += (
 +                "<div style='margin-bottom:10px'>"
 +                "<details>"
 +                "<summary style='cursor:pointer; font-weight:bold; color:#0066cc;'>Function Calls (click to expand)</summary>"
 +                "<div style='margin:10px; padding:10px; background-color:#f8f8f8; "
 +                "border:1px solid #ddd; border-radius:4px; white-space:pre-wrap; font-size:14px; color:#333;'>"
 +                f"{chr(10).join(function_calls)}"
 +                "</div></details></div>"
 +            )
 +
 +        html_output += (
 +            "<div style='margin-bottom:20px'>"
 +            f"<div style='font-weight:bold'>{agent_name or 'PriceAgent'}:</div>"
 +            f"<div style='margin-left:20px; white-space:pre-wrap'>{''.join(full_response)}</div></div><hr>"
 +        )
 +
 +        display(HTML(html_output))
 +
 +await main()
 +
 +</code>
 +
 +
 +
 +Testing 
 +
 +<code>
 +
 +async def interactive_conversation():
 +    """Interactive conversation loop with memory"""
 +    thread: ChatHistoryAgentThread | None = None
 +    print("🤖 PriceAgent: Hello! I can help you with price information. Type 'quit' to exit.")
 +    print("Ask me about egg prices, inflation rates, or consumer price index data.")
 +    print("-" * 60)
 +    
 +    while True:
 +        try:
 +            # Get user input
 +            user_input = input("\n👤 You: ").strip()
 +            
 +            if user_input.lower() in ['quit', 'exit', 'bye']:
 +                print("🤖 PriceAgent: Goodbye! Thanks for chatting with me.")
 +                break
 +            
 +            if not user_input:
 +                continue
 +                
 +            print("🤖 PriceAgent: ", end="", flush=True)
 +            
 +            # 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  # Maintain conversation memory
 +                content_items = list(response.items)
 +                
 +                for item in content_items:
 +                    if isinstance(item, FunctionCallContent):
 +                        if item.function_name:
 +                            current_function_name = item.function_name
 +                        
 +                        # Accumulate arguments (streamed in chunks)
 +                        if isinstance(item.arguments, str):
 +                            argument_buffer += item.arguments
 +                            
 +                    elif isinstance(item, FunctionResultContent):
 +                        # 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, indent=2)
 +                            except Exception:
 +                                pass
 +                            
 +                            function_calls.append({
 +                                'function': current_function_name,
 +                                'args': formatted_args,
 +                                'result': str(item.result)
 +                            })
 +                            
 +                            current_function_name = None
 +                            argument_buffer = ""
 +                            
 +                    elif isinstance(item, StreamingTextContent) and item.text:
 +                        # Print response as it streams
 +                        print(item.text, end="", flush=True)
 +                        full_response.append(item.text)
 +            
 +            print()  # New line after streaming response
 +            
 +            # Optionally show function calls in verbose mode
 +            if function_calls and False:  # Set to True to see function calls
 +                print("\n🔧 Function calls:")
 +                for call in function_calls:
 +                    print(f"  → {call['function']}({call['args']})")
 +                    print(f"    Result: {call['result']}")
 +                    
 +        except KeyboardInterrupt:
 +            print("\n🤖 PriceAgent: Conversation interrupted. Goodbye!")
 +            break
 +        except Exception as e:
 +            print(f"\n❌ Error: {e}")
 +            print("Please try again.")
 +
 +# Run the interactive conversation
 +await interactive_conversation()
 </code> </code>
  
wiki/ai/agentic_rag.1757707105.txt.gz · Last modified: by ymurugesan