|
11 | 11 | AgentMiddleware, |
12 | 12 | BaseChatClient, |
13 | 13 | ChatMiddleware, |
| 14 | + ChatOptions, |
14 | 15 | ContextProvider, |
15 | 16 | FunctionTool, |
16 | 17 | ToolMode, |
@@ -441,32 +442,61 @@ def build(self) -> Agent: |
441 | 442 | async with agent: |
442 | 443 | response = await agent.run("Hello!") |
443 | 444 | """ |
| 445 | + # Build default_options from model parameters |
| 446 | + options_dict: dict[str, Any] = {} |
| 447 | + if self._frequency_penalty is not None: |
| 448 | + options_dict["frequency_penalty"] = self._frequency_penalty |
| 449 | + if self._logit_bias is not None: |
| 450 | + options_dict["logit_bias"] = self._logit_bias |
| 451 | + if self._max_tokens is not None: |
| 452 | + options_dict["max_tokens"] = self._max_tokens |
| 453 | + if self._metadata is not None: |
| 454 | + options_dict["metadata"] = self._metadata |
| 455 | + if self._model_id is not None: |
| 456 | + options_dict["model"] = self._model_id |
| 457 | + if self._presence_penalty is not None: |
| 458 | + options_dict["presence_penalty"] = self._presence_penalty |
| 459 | + if self._response_format is not None: |
| 460 | + options_dict["response_format"] = self._response_format |
| 461 | + if self._seed is not None: |
| 462 | + options_dict["seed"] = self._seed |
| 463 | + if self._stop is not None: |
| 464 | + options_dict["stop"] = self._stop |
| 465 | + if self._store is not None: |
| 466 | + options_dict["store"] = self._store |
| 467 | + if self._temperature is not None: |
| 468 | + options_dict["temperature"] = self._temperature |
| 469 | + if self._tool_choice is not None: |
| 470 | + options_dict["tool_choice"] = self._tool_choice |
| 471 | + if self._top_p is not None: |
| 472 | + options_dict["top_p"] = self._top_p |
| 473 | + if self._user is not None: |
| 474 | + options_dict["user"] = self._user |
| 475 | + if self._additional_chat_options: |
| 476 | + options_dict.update(self._additional_chat_options) |
| 477 | + |
| 478 | + default_options = ChatOptions(**options_dict) if options_dict else None |
| 479 | + |
| 480 | + # Agent expects context_providers as a Sequence; wrap single instance in a list |
| 481 | + ctx_providers = self._context_providers |
| 482 | + if ctx_providers is not None and not isinstance(ctx_providers, list): |
| 483 | + ctx_providers = [ctx_providers] |
| 484 | + |
| 485 | + # Agent expects middleware as a Sequence; wrap single instance in a list |
| 486 | + mw = self._middleware |
| 487 | + if mw is not None and not isinstance(mw, list): |
| 488 | + mw = [mw] |
| 489 | + |
444 | 490 | return Agent( |
445 | | - chat_client=self._chat_client, |
| 491 | + self._chat_client, |
446 | 492 | instructions=self._instructions, |
447 | 493 | id=self._id, |
448 | 494 | name=self._name, |
449 | 495 | description=self._description, |
450 | | - chat_message_store_factory=self._chat_message_store_factory, |
451 | | - conversation_id=self._conversation_id, |
452 | | - context_providers=self._context_providers, |
453 | | - middleware=self._middleware, |
454 | | - frequency_penalty=self._frequency_penalty, |
455 | | - logit_bias=self._logit_bias, |
456 | | - max_tokens=self._max_tokens, |
457 | | - metadata=self._metadata, |
458 | | - model_id=self._model_id, |
459 | | - presence_penalty=self._presence_penalty, |
460 | | - response_format=self._response_format, |
461 | | - seed=self._seed, |
462 | | - stop=self._stop, |
463 | | - store=self._store, |
464 | | - temperature=self._temperature, |
465 | | - tool_choice=self._tool_choice, |
466 | 496 | tools=self._tools, |
467 | | - top_p=self._top_p, |
468 | | - user=self._user, |
469 | | - additional_chat_options=self._additional_chat_options, |
| 497 | + default_options=default_options, |
| 498 | + context_providers=ctx_providers, |
| 499 | + middleware=mw, |
470 | 500 | **self._kwargs, |
471 | 501 | ) |
472 | 502 |
|
@@ -755,31 +785,60 @@ def create_agent( |
755 | 785 | ``async with`` to ensure proper initialization and cleanup via the Agent's |
756 | 786 | async context manager protocol. |
757 | 787 | """ |
| 788 | + # Build default_options from model parameters |
| 789 | + opts: dict[str, Any] = {} |
| 790 | + if frequency_penalty is not None: |
| 791 | + opts["frequency_penalty"] = frequency_penalty |
| 792 | + if logit_bias is not None: |
| 793 | + opts["logit_bias"] = logit_bias |
| 794 | + if max_tokens is not None: |
| 795 | + opts["max_tokens"] = max_tokens |
| 796 | + if metadata is not None: |
| 797 | + opts["metadata"] = metadata |
| 798 | + if model_id is not None: |
| 799 | + opts["model"] = model_id |
| 800 | + if presence_penalty is not None: |
| 801 | + opts["presence_penalty"] = presence_penalty |
| 802 | + if response_format is not None: |
| 803 | + opts["response_format"] = response_format |
| 804 | + if seed is not None: |
| 805 | + opts["seed"] = seed |
| 806 | + if stop is not None: |
| 807 | + opts["stop"] = stop |
| 808 | + if store is not None: |
| 809 | + opts["store"] = store |
| 810 | + if temperature is not None: |
| 811 | + opts["temperature"] = temperature |
| 812 | + if tool_choice is not None: |
| 813 | + opts["tool_choice"] = tool_choice |
| 814 | + if top_p is not None: |
| 815 | + opts["top_p"] = top_p |
| 816 | + if user is not None: |
| 817 | + opts["user"] = user |
| 818 | + if additional_chat_options: |
| 819 | + opts.update(additional_chat_options) |
| 820 | + |
| 821 | + default_options = ChatOptions(**opts) if opts else None |
| 822 | + |
| 823 | + # Agent expects context_providers as a Sequence; wrap single instance in a list |
| 824 | + ctx_providers = context_providers |
| 825 | + if ctx_providers is not None and not isinstance(ctx_providers, list): |
| 826 | + ctx_providers = [ctx_providers] |
| 827 | + |
| 828 | + # Agent expects middleware as a Sequence; wrap single instance in a list |
| 829 | + mw = middleware |
| 830 | + if mw is not None and not isinstance(mw, list): |
| 831 | + mw = [mw] |
| 832 | + |
758 | 833 | return Agent( |
759 | | - chat_client=chat_client, |
| 834 | + chat_client, |
760 | 835 | instructions=instructions, |
761 | 836 | id=id, |
762 | 837 | name=name, |
763 | 838 | description=description, |
764 | | - chat_message_store_factory=chat_message_store_factory, |
765 | | - conversation_id=conversation_id, |
766 | | - context_providers=context_providers, |
767 | | - middleware=middleware, |
768 | | - frequency_penalty=frequency_penalty, |
769 | | - logit_bias=logit_bias, |
770 | | - max_tokens=max_tokens, |
771 | | - metadata=metadata, |
772 | | - model_id=model_id, |
773 | | - presence_penalty=presence_penalty, |
774 | | - response_format=response_format, |
775 | | - seed=seed, |
776 | | - stop=stop, |
777 | | - store=store, |
778 | | - temperature=temperature, |
779 | | - tool_choice=tool_choice, |
780 | 839 | tools=tools, |
781 | | - top_p=top_p, |
782 | | - user=user, |
783 | | - additional_chat_options=additional_chat_options, |
| 840 | + default_options=default_options, |
| 841 | + context_providers=ctx_providers, |
| 842 | + middleware=mw, |
784 | 843 | **kwargs, |
785 | 844 | ) |
0 commit comments