chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
from pydantic import SecretStr
|
||||
|
||||
from semantic_kernel.kernel_pydantic import HttpsUrl, KernelBaseSettings
|
||||
|
||||
|
||||
class AzureKeyVaultSettings(KernelBaseSettings):
|
||||
"""Azure Key Vault model settings
|
||||
|
||||
Optional:
|
||||
- vault_url: HttpsUrl - Azure Key Vault URL
|
||||
(Env var AZURE_KEY_VAULT_VAULT_URL)
|
||||
- client_id: str - Azure Key Vault client ID
|
||||
(Env var AZURE_KEY_VAULT_CLIENT_ID)
|
||||
- client_secret: SecretStr - Azure Key Vault client secret
|
||||
(Env var AZURE_KEY_VAULT_CLIENT_SECRET)
|
||||
"""
|
||||
|
||||
env_prefix: ClassVar[str] = "AZURE_KEY_VAULT_"
|
||||
|
||||
endpoint: HttpsUrl
|
||||
client_id: str
|
||||
client_secret: SecretStr
|
||||
@@ -0,0 +1,31 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from azure.identity import AzureCliCredential
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion
|
||||
from semantic_kernel.core_plugins.sessions_python_tool.sessions_python_plugin import SessionsPythonTool
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
|
||||
async def main():
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "python-code-interpreter"
|
||||
credential = AzureCliCredential()
|
||||
chat_service = AzureChatCompletion(service_id=service_id, credential=credential)
|
||||
kernel.add_service(chat_service)
|
||||
|
||||
python_code_interpreter = SessionsPythonTool(credential=credential)
|
||||
|
||||
sessions_tool = kernel.add_plugin(python_code_interpreter, "PythonCodeInterpreter")
|
||||
|
||||
code = "import json\n\ndef add_numbers(a, b):\n return a + b\n\nargs = '{\"a\": 1, \"b\": 1}'\nargs_dict = json.loads(args)\nprint(add_numbers(args_dict['a'], args_dict['b']))" # noqa: E501
|
||||
result = await kernel.invoke(sessions_tool["execute_code"], code=code)
|
||||
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,47 @@
|
||||
# Crew AI Plugin for Semantic Kernel
|
||||
|
||||
This sample demonstrates how to integrate with [Crew AI Enterprise](https://app.crewai.com/) crews in Semantic Kernel.
|
||||
|
||||
## Requirements
|
||||
|
||||
Before running this sample you need to have a Crew deployed to the Crew AI Enterprise cloud. Many pre-built Crew templates can be found [here](https://app.crewai.com/crewai_plus/templates). You will need the following information from your deployed Crew:
|
||||
|
||||
- endpoint: The base URL for your Crew.
|
||||
- authentication token: The authentication token for your Crew
|
||||
- required inputs: Most Crews have a set of required inputs that need to provided when kicking off the Crew and those input names, types, and values need to be known.
|
||||
|
||||
- ## Using the Crew Plugin
|
||||
|
||||
Once configured, the `CrewAIEnterprise` class can be used directly by calling methods on it, or can be used to generate a Semantic Kernel plugin with inputs that match those of your Crew. Generating a plugin is useful for scenarios when you want an LLM to be able to invoke your Crew as a tool.
|
||||
|
||||
## Running the sample
|
||||
|
||||
1. Deploy your Crew to the Crew Enterprise cloud.
|
||||
1. Gather the required information listed above.
|
||||
1. Create environment variables or use your .env file to define your Crew's endpoint and token as:
|
||||
|
||||
```md
|
||||
CREW_AI_ENDPOINT="{Your Crew's endpoint}"
|
||||
CREW_AI_TOKEN="{Your Crew's authentication token}"
|
||||
```
|
||||
|
||||
1. In [crew_ai_plugin.py](./crew_ai_plugin.py) find the section that defines the Crew's required inputs and modify it to match your Crew's inputs. The input descriptions and types are critical to help LLMs understand the inputs semantic meaning so that it can accurately call the plugin. The sample is based on the `Enterprise Content Marketing Crew` template which has two required inputs, `company` and `topic`.
|
||||
|
||||
```python
|
||||
# The required inputs for the Crew must be known in advance. This example is modeled after the
|
||||
# Enterprise Content Marketing Crew Template and requires string inputs for the company and topic.
|
||||
# We need to describe the type and purpose of each input to allow the LLM to invoke the crew as expected.
|
||||
crew_plugin_definitions = [
|
||||
KernelParameterMetadata(
|
||||
name="company",
|
||||
type="string",
|
||||
description="The name of the company that should be researched",
|
||||
is_required=True,
|
||||
),
|
||||
KernelParameterMetadata(
|
||||
name="topic", type="string", description="The topic that should be researched", is_required=True
|
||||
),
|
||||
]
|
||||
```
|
||||
|
||||
1. Run the sample. Notice that the sample invokes (kicks-off) the Crew twice, once directly by calling the `kickoff` method and once by creating a plugin and invoking it.
|
||||
@@ -0,0 +1,140 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from samples.concepts.setup.chat_completion_services import Services, get_chat_completion_service_and_request_settings
|
||||
from semantic_kernel import Kernel
|
||||
from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
|
||||
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
|
||||
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.core_plugins.crew_ai import CrewAIEnterprise
|
||||
from semantic_kernel.functions.kernel_parameter_metadata import KernelParameterMetadata
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
async def using_crew_ai_enterprise():
|
||||
# Create an instance of the CrewAI Enterprise Crew
|
||||
async with CrewAIEnterprise() as crew:
|
||||
#####################################################################
|
||||
# Using the CrewAI Enterprise Crew directly #
|
||||
#####################################################################
|
||||
|
||||
# The required inputs for the Crew must be known in advance. This example is modeled after the
|
||||
# Enterprise Content Marketing Crew Template and requires the following inputs:
|
||||
inputs = {"company": "CrewAI", "topic": "Agentic products for consumers"}
|
||||
|
||||
# Invoke directly with our inputs
|
||||
kickoff_id = await crew.kickoff(inputs)
|
||||
print(f"CrewAI Enterprise Crew kicked off with ID: {kickoff_id}")
|
||||
|
||||
# Wait for completion
|
||||
result = await crew.wait_for_crew_completion(kickoff_id)
|
||||
print("CrewAI Enterprise Crew completed with the following result:")
|
||||
print(result)
|
||||
|
||||
#####################################################################
|
||||
# Using the CrewAI Enterprise as a Plugin #
|
||||
#####################################################################
|
||||
|
||||
# Define the description of the Crew. This will used as the semantic description of the plugin.
|
||||
crew_description = (
|
||||
"Conducts thorough research on the specified company and topic to identify emerging trends,"
|
||||
"analyze competitor strategies, and gather data-driven insights."
|
||||
)
|
||||
|
||||
# The required inputs for the Crew must be known in advance. This example is modeled after the
|
||||
# Enterprise Content Marketing Crew Template and requires string inputs for the company and topic.
|
||||
# We need to describe the type and purpose of each input to allow the LLM to invoke the crew as expected.
|
||||
crew_input_parameters = [
|
||||
KernelParameterMetadata(
|
||||
name="company",
|
||||
type="string",
|
||||
type_object=str,
|
||||
description="The name of the company that should be researched",
|
||||
is_required=True,
|
||||
),
|
||||
KernelParameterMetadata(
|
||||
name="topic",
|
||||
type="string",
|
||||
type_object=str,
|
||||
description="The topic that should be researched",
|
||||
is_required=True,
|
||||
),
|
||||
]
|
||||
|
||||
# Create the CrewAI Plugin. This builds a plugin that can be added to the Kernel and invoked like any other
|
||||
# plugin. The plugin will contain the following functions:
|
||||
# - kickoff: Starts the Crew with the specified inputs and returns the Id of the scheduled kickoff.
|
||||
# - kickoff_and_wait: Starts the Crew with the specified inputs and waits for the Crew to complete before
|
||||
# returning the result.
|
||||
# - wait_for_completion: Waits for the specified Crew kickoff to complete and returns the result.
|
||||
# - get_status: Gets the status of the specified Crew kickoff.
|
||||
crew_plugin = crew.create_kernel_plugin(
|
||||
name="EnterpriseContentMarketingCrew",
|
||||
description=crew_description,
|
||||
parameters=crew_input_parameters,
|
||||
)
|
||||
|
||||
# Configure the kernel for chat completion and add the CrewAI plugin.
|
||||
kernel, chat_completion, settings = configure_kernel_for_chat()
|
||||
kernel.add_plugin(crew_plugin)
|
||||
|
||||
# Create a chat history to store the system message, initial messages, and the conversation.
|
||||
history = ChatHistory()
|
||||
history.add_system_message("You are an AI assistant that can help me with research.")
|
||||
history.add_user_message(
|
||||
"I'm looking for emerging marketplace trends about Crew AI and their concumer AI products."
|
||||
)
|
||||
|
||||
# Invoke the chat completion service with enough information for the CrewAI plugin to be invoked.
|
||||
response = await chat_completion.get_chat_message_content(history, settings, kernel=kernel)
|
||||
print(response)
|
||||
|
||||
# expected output:
|
||||
# INFO:semantic_kernel.connectors.ai.open_ai.services.open_ai_handler:OpenAI usage: ...
|
||||
# INFO:semantic_kernel.connectors.ai.chat_completion_client_base:processing 1 tool calls in parallel.
|
||||
# INFO:semantic_kernel.kernel:Calling EnterpriseContentMarketingCrew-kickoff_and_wait function with args:
|
||||
# {"company":"Crew AI","topic":"emerging marketplace trends in consumer AI products"}
|
||||
# INFO:semantic_kernel.functions.kernel_function:Function EnterpriseContentMarketingCrew-kickoff_and_wait
|
||||
# invoking.
|
||||
# INFO:semantic_kernel.core_plugins.crew_ai.crew_ai_enterprise:CrewAI Crew kicked off with Id: *****
|
||||
# INFO:semantic_kernel.core_plugins.crew_ai.crew_ai_enterprise:CrewAI Crew with kickoff Id: ***** completed with
|
||||
# status: SUCCESS
|
||||
# INFO:semantic_kernel.functions.kernel_function:Function EnterpriseContentMarketingCrew-kickoff_and_wait
|
||||
# succeeded.
|
||||
# Here are some emerging marketplace trends related to Crew AI and their consumer AI products, along with
|
||||
# suggested content pieces to explore these trends: ...
|
||||
|
||||
|
||||
def configure_kernel_for_chat() -> tuple[Kernel, ChatCompletionClientBase, PromptExecutionSettings]:
|
||||
kernel = Kernel()
|
||||
|
||||
# You can select from the following chat completion services that support function calling:
|
||||
# - Services.OPENAI
|
||||
# - Services.AZURE_OPENAI
|
||||
# - Services.AZURE_AI_INFERENCE
|
||||
# - Services.ANTHROPIC
|
||||
# - Services.BEDROCK
|
||||
# - Services.GOOGLE_AI
|
||||
# - Services.MISTRAL_AI
|
||||
# - Services.OLLAMA
|
||||
# - Services.ONNX
|
||||
# - Services.VERTEX_AI
|
||||
# - Services.DEEPSEEK
|
||||
# Please make sure you have configured your environment correctly for the selected chat completion service.
|
||||
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI)
|
||||
|
||||
# Configure the function choice behavior. Here, we set it to Auto, where auto_invoke=True by default.
|
||||
# With `auto_invoke=True`, the model will automatically choose and call functions as needed.
|
||||
request_settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
|
||||
|
||||
# Pass the request settings to the kernel arguments.
|
||||
kernel.add_service(chat_completion_service)
|
||||
return kernel, chat_completion_service, request_settings
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(using_crew_ai_enterprise())
|
||||
@@ -0,0 +1,134 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
from typing import Annotated
|
||||
|
||||
from azure.identity import AzureCliCredential
|
||||
|
||||
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
|
||||
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion
|
||||
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import (
|
||||
OpenAIChatPromptExecutionSettings,
|
||||
)
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.contents.function_call_content import FunctionCallContent
|
||||
from semantic_kernel.core_plugins.time_plugin import TimePlugin
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
from semantic_kernel.functions.kernel_function_decorator import kernel_function
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
|
||||
class WeatherPlugin:
|
||||
"""A sample plugin that provides weather information for cities."""
|
||||
|
||||
@kernel_function(name="get_weather_for_city", description="Get the weather for a city")
|
||||
def get_weather_for_city(self, city: Annotated[str, "The input city"]) -> Annotated[str, "The output is a string"]:
|
||||
if city == "Boston":
|
||||
return "61 and rainy"
|
||||
if city == "London":
|
||||
return "55 and cloudy"
|
||||
if city == "Miami":
|
||||
return "80 and sunny"
|
||||
if city == "Paris":
|
||||
return "60 and rainy"
|
||||
if city == "Tokyo":
|
||||
return "50 and sunny"
|
||||
if city == "Sydney":
|
||||
return "75 and sunny"
|
||||
if city == "Tel Aviv":
|
||||
return "80 and sunny"
|
||||
return "31 and snowing"
|
||||
|
||||
|
||||
async def main():
|
||||
kernel = Kernel()
|
||||
|
||||
use_azure_openai = False
|
||||
service_id = "function_calling"
|
||||
if use_azure_openai:
|
||||
# Please make sure your AzureOpenAI Deployment allows for function calling
|
||||
ai_service = AzureChatCompletion(service_id=service_id, credential=AzureCliCredential())
|
||||
else:
|
||||
ai_service = OpenAIChatCompletion(
|
||||
service_id=service_id,
|
||||
ai_model_id="gpt-3.5-turbo",
|
||||
)
|
||||
kernel.add_service(ai_service)
|
||||
|
||||
kernel.add_plugin(TimePlugin(), plugin_name="time")
|
||||
kernel.add_plugin(WeatherPlugin(), plugin_name="weather")
|
||||
|
||||
# Example 1: Use automated function calling with a non-streaming prompt
|
||||
print("========== Example 1: Use automated function calling with a non-streaming prompt ==========")
|
||||
settings: OpenAIChatPromptExecutionSettings = kernel.get_prompt_execution_settings_from_service_id(
|
||||
service_id=service_id
|
||||
)
|
||||
settings.function_choice_behavior = FunctionChoiceBehavior.Auto(filters={"included_plugins": ["weather", "time"]})
|
||||
|
||||
print(
|
||||
await kernel.invoke_prompt(
|
||||
function_name="prompt_test",
|
||||
plugin_name="weather_test",
|
||||
prompt="Given the current time of day and weather, what is the likely color of the sky in Boston?",
|
||||
settings=settings,
|
||||
)
|
||||
)
|
||||
|
||||
# Example 2: Use automated function calling with a streaming prompt
|
||||
print("========== Example 2: Use automated function calling with a streaming prompt ==========")
|
||||
settings: OpenAIChatPromptExecutionSettings = kernel.get_prompt_execution_settings_from_service_id(
|
||||
service_id=service_id
|
||||
)
|
||||
settings.function_choice_behavior = FunctionChoiceBehavior.Auto(filters={"included_plugins": ["weather", "time"]})
|
||||
|
||||
result = kernel.invoke_prompt_stream(
|
||||
function_name="prompt_test",
|
||||
plugin_name="weather_test",
|
||||
prompt="Given the current time of day and weather, what is the likely color of the sky in Boston?",
|
||||
settings=settings,
|
||||
)
|
||||
|
||||
async for message in result:
|
||||
print(str(message[0]), end="")
|
||||
print("")
|
||||
|
||||
# Example 3: Use manual function calling with a non-streaming prompt
|
||||
print("========== Example 3: Use manual function calling with a non-streaming prompt ==========")
|
||||
|
||||
chat: OpenAIChatCompletion | AzureChatCompletion = kernel.get_service(service_id)
|
||||
chat_history = ChatHistory()
|
||||
settings: OpenAIChatPromptExecutionSettings = kernel.get_prompt_execution_settings_from_service_id(
|
||||
service_id=service_id
|
||||
)
|
||||
settings.function_choice_behavior = FunctionChoiceBehavior.Auto(
|
||||
auto_invoke=False, filters={"included_plugins": ["weather", "time"]}
|
||||
)
|
||||
chat_history.add_user_message(
|
||||
"Given the current time of day and weather, what is the likely color of the sky in Boston?"
|
||||
)
|
||||
|
||||
while True:
|
||||
# The result is a list of ChatMessageContent objects, grab the first one
|
||||
result = await chat.get_chat_message_contents(chat_history=chat_history, settings=settings, kernel=kernel)
|
||||
result = result[0]
|
||||
|
||||
if result.content:
|
||||
print(result.content)
|
||||
|
||||
if not result.items or not any(isinstance(item, FunctionCallContent) for item in result.items):
|
||||
break
|
||||
|
||||
chat_history.add_message(result)
|
||||
for item in result.items:
|
||||
await kernel.invoke_function_call(
|
||||
function_call=item,
|
||||
chat_history=chat_history,
|
||||
arguments=KernelArguments(),
|
||||
function_call_count=1,
|
||||
request_index=0,
|
||||
function_behavior=settings.function_choice_behavior,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,11 @@
|
||||
### Running the OpenAPI syntax example
|
||||
|
||||
For more generic setup instructions, including how to install the `uv` tool, see the [main README](../../../../DEV_SETUP.md).
|
||||
|
||||
1. In a terminal, navigate to `semantic_kernel/python/samples/concepts/plugins/openapi`.
|
||||
|
||||
2. Run `uv sync` followed by `source .venv/bin/activate` to enter the virtual environment (depending on the os, the activate script may be in a different location).
|
||||
|
||||
3. Start the server by running `python openapi_server.py`.
|
||||
|
||||
4. In another terminal, do steps 1 & 2. Then, run `python openapi_client.py`, which will register a plugin representing the API defined in openapi.yaml
|
||||
@@ -0,0 +1,44 @@
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: Test API
|
||||
version: 1.0.0
|
||||
servers:
|
||||
- url: http://localhost:8080
|
||||
paths:
|
||||
/{name}:
|
||||
post:
|
||||
summary: Hello World
|
||||
operationId: helloWorld
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
input:
|
||||
type: string
|
||||
description: The input of the request
|
||||
example: Howdy
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
parameters:
|
||||
- name: name
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Your name
|
||||
- name: Header
|
||||
in: header
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: The header
|
||||
- name: q
|
||||
in: query
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
description: The query parameter
|
||||
@@ -0,0 +1,36 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from semantic_kernel import Kernel
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
|
||||
|
||||
async def main():
|
||||
"""OpenAPI Sample Client"""
|
||||
kernel = Kernel()
|
||||
|
||||
spec_path = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),
|
||||
"plugins",
|
||||
"openapi",
|
||||
"openapi.yaml",
|
||||
)
|
||||
|
||||
openapi_plugin = kernel.add_plugin_from_openapi(plugin_name="openApiPlugin", openapi_document_path=spec_path)
|
||||
|
||||
arguments = KernelArguments(
|
||||
input="hello world",
|
||||
name="John",
|
||||
q=0.7,
|
||||
Header="example-header",
|
||||
)
|
||||
|
||||
result = await kernel.invoke(openapi_plugin["helloWorld"], arguments=arguments)
|
||||
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,26 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
"""OpenAPI Sample Server"""
|
||||
routes = web.RouteTableDef()
|
||||
|
||||
|
||||
@routes.post("/{name}")
|
||||
async def hello(request):
|
||||
# Get path parameters
|
||||
name = request.match_info.get("name", "")
|
||||
# Get query parameters
|
||||
q = request.rel_url.query.get("q", "")
|
||||
# Get body
|
||||
body = await request.json()
|
||||
# Get headers
|
||||
headers = request.headers
|
||||
return web.Response(text=f"Hello, {name}: q={q}, body={body}, headers={headers}")
|
||||
|
||||
|
||||
app = web.Application()
|
||||
app.add_routes(routes)
|
||||
|
||||
if __name__ == "__main__":
|
||||
web.run_app(app)
|
||||
@@ -0,0 +1,33 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from semantic_kernel import Kernel
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
|
||||
from semantic_kernel.functions import KernelArguments
|
||||
|
||||
|
||||
async def main():
|
||||
kernel = Kernel()
|
||||
|
||||
model = "gpt-3.5-turbo-instruct"
|
||||
service_id = model
|
||||
|
||||
# Configure AI service used by the kernel
|
||||
kernel.add_service(
|
||||
OpenAITextCompletion(service_id=service_id, ai_model_id=model),
|
||||
)
|
||||
|
||||
# note: using plugins from the samples folder
|
||||
plugins_directory = os.path.join(__file__, "../../../../../prompt_template_samples/")
|
||||
plugin = kernel.add_plugin(parent_directory=plugins_directory, plugin_name="FunPlugin")
|
||||
|
||||
arguments = KernelArguments(input="time travel to dinosaur age", style="super silly")
|
||||
|
||||
result = await kernel.invoke(plugin["Joke"], arguments)
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user