-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PY] feat: Chat Completion - Tools (#1853)
## Linked issues closes: #1699 ## Details - providing feature support w/ function calling in [chat completions](https://platform.openai.com/docs/guides/function-calling) ## Attestation Checklist - [x] My code follows the style guidelines of this project - I have checked for/fixed spelling, linting, and other errors - I have commented my code for clarity - I have made corresponding changes to the documentation (updating the doc strings in the code is sufficient) - My changes generate no new warnings - I have added tests that validates my changes, and provides sufficient test coverage. I have tested with: - Local testing - E2E testing in Teams - New and existing unit tests pass locally with my changes --------- Co-authored-by: Alex Acebo <aacebowork@gmail.com>
- Loading branch information
Showing
21 changed files
with
832 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
python/packages/ai/teams/ai/augmentations/tools_augmentation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import json | ||
from typing import List, Optional | ||
|
||
from botbuilder.core import TurnContext | ||
|
||
from ...state import MemoryBase | ||
from ..models.prompt_response import PromptResponse | ||
from ..planners.plan import ( | ||
Plan, | ||
PredictedCommand, | ||
PredictedDoCommand, | ||
PredictedSayCommand, | ||
) | ||
from ..prompts.sections.prompt_section import PromptSection | ||
from ..tokenizers.tokenizer import Tokenizer | ||
from ..validators.validation import Validation | ||
from .augmentation import Augmentation | ||
|
||
|
||
class ToolsAugmentation(Augmentation[str]): | ||
""" | ||
A server-side 'tools' augmentation. | ||
""" | ||
|
||
def create_prompt_section(self) -> Optional[PromptSection]: | ||
""" | ||
Creates an optional prompt section for the augmentation. | ||
""" | ||
return None | ||
|
||
async def validate_response( | ||
self, | ||
context: TurnContext, | ||
memory: MemoryBase, | ||
tokenizer: Tokenizer, | ||
response: PromptResponse[str], | ||
remaining_attempts: int, | ||
) -> Validation: | ||
""" | ||
Validates a response to a prompt. | ||
Args: | ||
context (TurnContext): Context for the current turn of conversation. | ||
memory (MemoryBase): Interface for accessing state variables. | ||
tokenizer (Tokenizer): Tokenizer to use for encoding/decoding text. | ||
response (PromptResponse[str]): Response to validate. | ||
remaining_attempts (int): Nubmer of remaining attempts to validate the response. | ||
Returns: | ||
Validation: A 'Validation' object. | ||
""" | ||
return Validation(valid=True) | ||
|
||
async def create_plan_from_response( | ||
self, | ||
turn_context: TurnContext, | ||
memory: MemoryBase, | ||
response: PromptResponse[str], | ||
) -> Plan: | ||
""" | ||
Creates a plan given validated response value. | ||
Args: | ||
turn_context (TurnContext): Context for the current turn of conversation. | ||
memory (MemoryBase): Interface for accessing state variables. | ||
response (PromptResponse[str]): | ||
The validated and transformed response for the prompt. | ||
Returns: | ||
Plan: The created plan. | ||
""" | ||
|
||
commands: List[PredictedCommand] = [] | ||
|
||
if response.message and response.message.action_calls: | ||
tool_calls = response.message.action_calls | ||
|
||
for tool in tool_calls: | ||
command = PredictedDoCommand( | ||
action=tool.function.name, | ||
parameters=json.loads(tool.function.arguments), | ||
action_id=tool.id, | ||
) | ||
commands.append(command) | ||
return Plan(commands=commands) | ||
|
||
return Plan(commands=[PredictedSayCommand(response=response.message)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.