chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from foundry_local import FoundryLocalManager
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAIChatPromptExecutionSettings
|
||||
from semantic_kernel.contents import ChatHistory
|
||||
|
||||
"""
|
||||
This samples demonstrates how to use the Foundry Local model with the OpenAIChatCompletion service.
|
||||
The Foundry Local model is a local model that can be used to run the OpenAIChatCompletion service.
|
||||
To use this sample, you need to install the Foundry Local SDK and service.
|
||||
For the service refer to this guide: https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-local/get-started
|
||||
To install the SDK, run the following command:
|
||||
`pip install foundry-local-sdk`
|
||||
"""
|
||||
|
||||
# The way Foundry Local works, is that it picks the right variant of a model based on the
|
||||
# hardware available on the machine. For example, if you have a GPU, it will pick the GPU variant
|
||||
# of the model. If you have a CPU, it will pick the CPU variant of the model.
|
||||
# The model alias is the name of the model that you want to use.
|
||||
model_alias = "phi-4-mini"
|
||||
manager = FoundryLocalManager(model_alias)
|
||||
# next, download the model to the machine
|
||||
manager.download_model(model_alias)
|
||||
# load the model into memory
|
||||
manager.load_model(model_alias)
|
||||
|
||||
service = OpenAIChatCompletion(
|
||||
ai_model_id=manager.get_model_info(model_alias).id,
|
||||
async_client=AsyncOpenAI(
|
||||
base_url=manager.endpoint,
|
||||
api_key=manager.api_key,
|
||||
),
|
||||
)
|
||||
# if needed, set the other parameters for the execution
|
||||
request_settings = OpenAIChatPromptExecutionSettings()
|
||||
# This is the system message that gives the chatbot its personality.
|
||||
system_message = """
|
||||
You are a chat bot. Your name is Mosscap and
|
||||
you have one goal: figure out what people need.
|
||||
Your full name, should you need to know it, is
|
||||
Splendid Speckled Mosscap. You communicate
|
||||
effectively, but you tend to answer with long
|
||||
flowery prose. Use the tools you have available!
|
||||
"""
|
||||
|
||||
# Create a chat history object with the system message.
|
||||
chat_history = ChatHistory(system_message=system_message)
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
# Add the user message to the chat history so that the chatbot can respond to it.
|
||||
chat_history.add_user_message(user_input)
|
||||
|
||||
# Get the chat message content from the chat completion service.
|
||||
response = await service.get_chat_message_content(
|
||||
chat_history=chat_history,
|
||||
settings=request_settings,
|
||||
)
|
||||
if response:
|
||||
print(f"Mosscap:> {response}")
|
||||
|
||||
# Add the chat message to the chat history to keep track of the conversation.
|
||||
chat_history.add_message(response)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Start the chat loop. The chat loop will continue until the user types "exit".
|
||||
chatting = True
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
User:> Why is the sky blue in one sentence?
|
||||
Mosscap:> The sky appears blue due to Rayleigh scattering, where shorter blue wavelengths of sunlight are scattered in
|
||||
all directions by the gases and particles in Earth's atmosphere more than other colors.
|
||||
"""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,83 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
# This concept sample shows how to use the OpenAI connector to create a
|
||||
# chat experience with a local model running in LM studio: https://lmstudio.ai/
|
||||
# Please follow the instructions here: https://lmstudio.ai/docs/local-server to set up LM studio.
|
||||
# The default model used in this sample is phi3 due to its compact size.
|
||||
|
||||
system_message = """
|
||||
You are a chat bot. Your name is Mosscap and
|
||||
you have one goal: figure out what people need.
|
||||
Your full name, should you need to know it, is
|
||||
Splendid Speckled Mosscap. You communicate
|
||||
effectively, but you tend to answer with long
|
||||
flowery prose.
|
||||
"""
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "local-gpt"
|
||||
|
||||
openAIClient: AsyncOpenAI = AsyncOpenAI(
|
||||
api_key="fake-key", # This cannot be an empty string, use a fake key
|
||||
base_url="http://localhost:1234/v1",
|
||||
)
|
||||
kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id="phi3", async_client=openAIClient))
|
||||
|
||||
settings = kernel.get_prompt_execution_settings_from_service_id(service_id)
|
||||
settings.max_tokens = 2000
|
||||
settings.temperature = 0.7
|
||||
settings.top_p = 0.8
|
||||
|
||||
chat_function = kernel.add_function(
|
||||
plugin_name="ChatBot",
|
||||
function_name="Chat",
|
||||
prompt="{{$chat_history}}{{$user_input}}",
|
||||
template_format="semantic-kernel",
|
||||
prompt_execution_settings=settings,
|
||||
)
|
||||
|
||||
chat_history = ChatHistory(system_message=system_message)
|
||||
chat_history.add_user_message("Hi there, who are you?")
|
||||
chat_history.add_assistant_message("I am Mosscap, a chat bot. I'm trying to figure out what people need")
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
answer = await kernel.invoke(chat_function, KernelArguments(user_input=user_input, chat_history=chat_history))
|
||||
chat_history.add_user_message(user_input)
|
||||
chat_history.add_assistant_message(str(answer))
|
||||
print(f"Mosscap:> {answer}")
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
chatting = True
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,62 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
|
||||
from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin
|
||||
from semantic_kernel.kernel import Kernel
|
||||
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
|
||||
from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore
|
||||
|
||||
# This concept sample shows how to use the OpenAI connector to add memory
|
||||
# to applications with a local embedding model running in LM studio: https://lmstudio.ai/
|
||||
# Please follow the instructions here: https://lmstudio.ai/docs/local-server to set up LM studio.
|
||||
# The default model used in this sample is from nomic.ai due to its compact size.
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "local-gpt"
|
||||
|
||||
openAIClient: AsyncOpenAI = AsyncOpenAI(
|
||||
api_key="fake_key", # This cannot be an empty string, use a fake key
|
||||
base_url="http://localhost:1234/v1",
|
||||
)
|
||||
kernel.add_service(
|
||||
OpenAITextEmbedding(
|
||||
service_id=service_id, ai_model_id="Nomic-embed-text-v1.5-Embedding-GGUF", async_client=openAIClient
|
||||
)
|
||||
)
|
||||
|
||||
memory = SemanticTextMemory(storage=VolatileMemoryStore(), embeddings_generator=kernel.get_service(service_id))
|
||||
kernel.add_plugin(TextMemoryPlugin(memory), "TextMemoryPlugin")
|
||||
|
||||
|
||||
async def populate_memory(memory: SemanticTextMemory, collection_id="generic") -> None:
|
||||
# Add some documents to the semantic memory
|
||||
await memory.save_information(collection=collection_id, id="info1", text="Your budget for 2024 is $100,000")
|
||||
await memory.save_information(collection=collection_id, id="info2", text="Your savings from 2023 are $50,000")
|
||||
await memory.save_information(collection=collection_id, id="info3", text="Your investments are $80,000")
|
||||
|
||||
|
||||
async def search_memory_examples(memory: SemanticTextMemory, collection_id="generic") -> None:
|
||||
questions = [
|
||||
"What is my budget for 2024?",
|
||||
"What are my savings from 2023?",
|
||||
"What are my investments?",
|
||||
]
|
||||
|
||||
for question in questions:
|
||||
print(f"Question: {question}")
|
||||
result = await memory.search(collection_id, question)
|
||||
print(f"Answer: {result[0].text}\n")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await populate_memory(memory)
|
||||
await search_memory_examples(memory)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,87 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
# This concept sample shows how to use the OpenAI connector with
|
||||
# a local model running in Ollama: https://github.com/ollama/ollama
|
||||
# A docker image is also available: https://hub.docker.com/r/ollama/ollama
|
||||
# The default model used in this sample is phi3 due to its compact size.
|
||||
# At the time of creating this sample, Ollama only provides experimental
|
||||
# compatibility with the `chat/completions` endpoint:
|
||||
# https://github.com/ollama/ollama/blob/main/docs/openai.md
|
||||
# Please follow the instructions in the Ollama repository to set up Ollama.
|
||||
|
||||
system_message = """
|
||||
You are a chat bot. Your name is Mosscap and
|
||||
you have one goal: figure out what people need.
|
||||
Your full name, should you need to know it, is
|
||||
Splendid Speckled Mosscap. You communicate
|
||||
effectively, but you tend to answer with long
|
||||
flowery prose.
|
||||
"""
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "local-gpt"
|
||||
|
||||
openAIClient: AsyncOpenAI = AsyncOpenAI(
|
||||
api_key="fake-key", # This cannot be an empty string, use a fake key
|
||||
base_url="http://localhost:11434/v1",
|
||||
)
|
||||
kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id="phi3", async_client=openAIClient))
|
||||
|
||||
settings = kernel.get_prompt_execution_settings_from_service_id(service_id)
|
||||
settings.max_tokens = 2000
|
||||
settings.temperature = 0.7
|
||||
settings.top_p = 0.8
|
||||
|
||||
chat_function = kernel.add_function(
|
||||
plugin_name="ChatBot",
|
||||
function_name="Chat",
|
||||
prompt="{{$chat_history}}{{$user_input}}",
|
||||
template_format="semantic-kernel",
|
||||
prompt_execution_settings=settings,
|
||||
)
|
||||
|
||||
chat_history = ChatHistory(system_message=system_message)
|
||||
chat_history.add_user_message("Hi there, who are you?")
|
||||
chat_history.add_assistant_message("I am Mosscap, a chat bot. I'm trying to figure out what people need")
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
answer = await kernel.invoke(chat_function, KernelArguments(user_input=user_input, chat_history=chat_history))
|
||||
chat_history.add_user_message(user_input)
|
||||
chat_history.add_assistant_message(str(answer))
|
||||
print(f"Mosscap:> {answer}")
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
chatting = True
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,75 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from semantic_kernel.connectors.ai.onnx import OnnxGenAIChatCompletion, OnnxGenAIPromptExecutionSettings
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
# This concept sample shows how to use the Onnx connector with
|
||||
# a local model running in Onnx
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "phi3"
|
||||
#############################################
|
||||
# Make sure to download an ONNX model
|
||||
# (https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx)
|
||||
# If onnxruntime-genai is used:
|
||||
# use the model stored in /cpu folder
|
||||
# If onnxruntime-genai-cuda is installed for gpu use:
|
||||
# use the model stored in /cuda folder
|
||||
# Then set ONNX_GEN_AI_CHAT_MODEL_FOLDER environment variable to the path to the model folder
|
||||
#############################################
|
||||
streaming = True
|
||||
|
||||
chat_completion = OnnxGenAIChatCompletion(ai_model_id=service_id, template="phi3")
|
||||
settings = OnnxGenAIPromptExecutionSettings()
|
||||
|
||||
system_message = """You are a helpful assistant."""
|
||||
chat_history = ChatHistory(system_message=system_message)
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
chat_history.add_user_message(user_input)
|
||||
if streaming:
|
||||
print("Mosscap:> ", end="")
|
||||
message = ""
|
||||
async for chunk in chat_completion.get_streaming_chat_message_content(
|
||||
chat_history=chat_history, settings=settings, kernel=kernel
|
||||
):
|
||||
if chunk:
|
||||
print(str(chunk), end="")
|
||||
message += str(chunk)
|
||||
chat_history.add_assistant_message(message)
|
||||
print("")
|
||||
else:
|
||||
answer = await chat_completion.get_chat_message_content(
|
||||
chat_history=chat_history, settings=settings, kernel=kernel
|
||||
)
|
||||
print(f"Mosscap:> {answer}")
|
||||
chat_history.add_message(answer)
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
chatting = True
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,91 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from semantic_kernel.connectors.ai.onnx import OnnxGenAIChatCompletion, OnnxGenAIPromptExecutionSettings
|
||||
from semantic_kernel.contents import AuthorRole, ChatHistory, ChatMessageContent, ImageContent
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
# This concept sample shows how to use the Onnx connector with
|
||||
# a local model running in Onnx
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "phi3"
|
||||
#############################################
|
||||
# Make sure to download an ONNX model
|
||||
# If onnxruntime-genai is used:
|
||||
# (https://huggingface.co/microsoft/Phi-3-vision-128k-instruct-onnx-cpu)
|
||||
# If onnxruntime-genai-cuda is installed for gpu use:
|
||||
# (https://huggingface.co/microsoft/Phi-3-vision-128k-instruct-onnx-gpu)
|
||||
# Then set ONNX_GEN_AI_CHAT_MODEL_FOLDER environment variable to the path to the model folder
|
||||
#############################################
|
||||
streaming = True
|
||||
|
||||
chat_completion = OnnxGenAIChatCompletion(ai_model_id=service_id, template="phi3v")
|
||||
|
||||
# Max length property is important to allocate RAM
|
||||
# If the value is too big, you ran out of memory
|
||||
# If the value is too small, your input is limited
|
||||
settings = OnnxGenAIPromptExecutionSettings(max_length=4096)
|
||||
|
||||
system_message = """
|
||||
You are a helpful assistant.
|
||||
You know about provided images and the history of the conversation.
|
||||
"""
|
||||
chat_history = ChatHistory(system_message=system_message)
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
chat_history.add_user_message(user_input)
|
||||
if streaming:
|
||||
print("Mosscap:> ", end="")
|
||||
message = ""
|
||||
async for chunk in chat_completion.get_streaming_chat_message_content(
|
||||
chat_history=chat_history, settings=settings, kernel=kernel
|
||||
):
|
||||
print(chunk.content, end="")
|
||||
if chunk.content:
|
||||
message += chunk.content
|
||||
chat_history.add_assistant_message(message)
|
||||
print("")
|
||||
else:
|
||||
answer = await chat_completion.get_chat_message_content(
|
||||
chat_history=chat_history, settings=settings, kernel=kernel
|
||||
)
|
||||
print(f"Mosscap:> {answer}")
|
||||
chat_history.add_message(message)
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
chatting = True
|
||||
image_path = input("Image Path (leave empty if no image): ")
|
||||
if image_path:
|
||||
chat_history.add_message(
|
||||
ChatMessageContent(
|
||||
role=AuthorRole.USER,
|
||||
items=[
|
||||
ImageContent.from_image_path(image_path=image_path),
|
||||
],
|
||||
),
|
||||
)
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,76 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from semantic_kernel.connectors.ai.onnx import OnnxGenAITextCompletion
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
from semantic_kernel.kernel import Kernel
|
||||
|
||||
# This concept sample shows how to use the Onnx connector with
|
||||
# a local model running in Onnx
|
||||
|
||||
kernel = Kernel()
|
||||
|
||||
service_id = "phi3"
|
||||
#############################################
|
||||
# Make sure to download an ONNX model
|
||||
# (https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx)
|
||||
# If onnxruntime-genai is used:
|
||||
# use the model stored in /cpu folder
|
||||
# If onnxruntime-genai-cuda is installed for gpu use:
|
||||
# use the model stored in /cuda folder
|
||||
# Then set ONNX_GEN_AI_TEXT_MODEL_FOLDER environment variable to the path to the model folder
|
||||
#############################################
|
||||
streaming = True
|
||||
|
||||
kernel.add_service(OnnxGenAITextCompletion(ai_model_id=service_id))
|
||||
|
||||
settings = kernel.get_prompt_execution_settings_from_service_id(service_id)
|
||||
|
||||
# Phi3 Model is using chat templates to generate responses
|
||||
# With the Chat Template the model understands
|
||||
# the context and roles of the conversation better
|
||||
# https://huggingface.co/microsoft/Phi-3-mini-4k-instruct#chat-format
|
||||
chat_function = kernel.add_function(
|
||||
plugin_name="ChatBot",
|
||||
function_name="Chat",
|
||||
prompt="<|user|>{{$user_input}}<|end|><|assistant|>",
|
||||
template_format="semantic-kernel",
|
||||
prompt_execution_settings=settings,
|
||||
)
|
||||
|
||||
|
||||
async def chat() -> bool:
|
||||
try:
|
||||
user_input = input("User:> ")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
except EOFError:
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if user_input == "exit":
|
||||
print("\n\nExiting chat...")
|
||||
return False
|
||||
|
||||
if streaming:
|
||||
print("Mosscap:> ", end="")
|
||||
async for chunk in kernel.invoke_stream(chat_function, KernelArguments(user_input=user_input)):
|
||||
print(chunk[0].text, end="")
|
||||
print("\n")
|
||||
else:
|
||||
answer = await kernel.invoke(chat_function, KernelArguments(user_input=user_input))
|
||||
print(f"Mosscap:> {answer}")
|
||||
return True
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
chatting = True
|
||||
while chatting:
|
||||
chatting = await chat()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user