forked from Azure/azure-functions-durable-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Move OpenAI decorator to DFApp
class
#6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2e91846
Scaffold some basic tests.
philliphoff b811548
Move OpenAI tests.
philliphoff c962be0
Move OpenAI tests.
philliphoff 18c9378
Add tests for tool use.
philliphoff 20994db
Merge branch 'durable-openai-agent' into philliphoff-add-initial-tests
philliphoff c0029dc
Fixup merge.
philliphoff dea3a53
Resolve annotations error.
philliphoff 3e9d95f
A better way to resolve linter.
philliphoff 09a1fcb
Move decorator to app class.
philliphoff 6d091b6
Rename functions.
philliphoff 95a9bfb
Merge branch 'durable-openai-agent' into philliphoff-move-decorator
philliphoff 968b889
Fix linting errors.
philliphoff 8d9d1fc
Resolve more linter errors.
philliphoff cfacc8a
More linting.
philliphoff 35163fc
Better?
philliphoff a634a87
Less text is more.
philliphoff 6c3bccf
More lines is more.
philliphoff 536ca85
Update sample.
philliphoff c39be4e
Separate activity logic from registration logic.
philliphoff ffd6ff2
Resolve linter warnings.
philliphoff f4274ca
Lines still too long.
philliphoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
azure/durable_functions/openai_agents/orchestrator_generator.py
This file contains hidden or 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,75 @@ | ||
import inspect | ||
from agents import ModelProvider, ModelResponse | ||
from agents.run import set_default_agent_runner | ||
from azure.durable_functions.models.DurableOrchestrationContext import DurableOrchestrationContext | ||
from azure.durable_functions.openai_agents.model_invocation_activity\ | ||
import ActivityModelInput, ModelInvoker | ||
from .runner import DurableOpenAIRunner | ||
from .exceptions import YieldException | ||
from .context import DurableAIAgentContext | ||
from .event_loop import ensure_event_loop | ||
|
||
|
||
async def durable_openai_agent_activity(input: str, model_provider: ModelProvider): | ||
"""Activity logic that handles OpenAI model invocations.""" | ||
activity_input = ActivityModelInput.from_json(input) | ||
|
||
model_invoker = ModelInvoker(model_provider=model_provider) | ||
result = await model_invoker.invoke_model_activity(activity_input) | ||
|
||
json_obj = ModelResponse.__pydantic_serializer__.to_json(result) | ||
return json_obj.decode() | ||
|
||
|
||
def durable_openai_agent_orchestrator_generator( | ||
func, | ||
durable_orchestration_context: DurableOrchestrationContext): | ||
"""Adapts the synchronous OpenAI Agents function to an Durable orchestrator generator.""" | ||
ensure_event_loop() | ||
durable_ai_agent_context = DurableAIAgentContext(durable_orchestration_context) | ||
durable_openai_runner = DurableOpenAIRunner(context=durable_ai_agent_context) | ||
set_default_agent_runner(durable_openai_runner) | ||
|
||
if inspect.isgeneratorfunction(func): | ||
gen = iter(func(durable_ai_agent_context)) | ||
try: | ||
# prime the subiterator | ||
value = next(gen) | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() | ||
while True: | ||
try: | ||
# send whatever was sent into us down to the subgenerator | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() | ||
sent = yield value | ||
except GeneratorExit: | ||
# ensure the subgenerator is closed | ||
if hasattr(gen, "close"): | ||
gen.close() | ||
raise | ||
except BaseException as exc: | ||
# forward thrown exceptions if possible | ||
if hasattr(gen, "throw"): | ||
value = gen.throw(type(exc), exc, exc.__traceback__) | ||
else: | ||
raise | ||
else: | ||
# normal path: forward .send (or .__next__) | ||
if hasattr(gen, "send"): | ||
value = gen.send(sent) | ||
else: | ||
value = next(gen) | ||
except StopIteration as e: | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() | ||
return e.value | ||
except YieldException as e: | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() | ||
yield e.task | ||
else: | ||
try: | ||
result = func(durable_ai_agent_context) | ||
return result | ||
except YieldException as e: | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() | ||
yield e.task | ||
finally: | ||
yield from durable_ai_agent_context._yield_and_clear_tasks() |
This file contains hidden or 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
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned about bringing this much of OpenAI-related code directly into this file. Until now, all the code related to this integration was under the openai_agents folder, and I believe there is value in keeping it this way. Obviously, the entry point (durable_openai_agent_orchestrator) belongs to this class, but perhaps it can delegate to code remaining under openai_agents?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, though I feel the app registration and "outer" activity/orchestration logic should live in this app type. I've moved the OpenAI-specific logic out.