Fixing typos (#1377)

### Motivation and Context
Fixing typos to unblock PRs.
This commit is contained in:
Lee Miller
2023-06-08 17:41:02 +00:00
committed by GitHub
parent 3bc0967b39
commit de74668637
8 changed files with 19260 additions and 3783 deletions
+3
View File
@@ -19,3 +19,6 @@ extend-exclude = [
[default.extend-words]
ACI = "ACI" # Azure Container Instance
[default.extend-identifiers]
ags = "ags" # Azure Graph Service
+1 -1
View File
@@ -15,7 +15,7 @@ commonly used terms
**Function** - A computational machine comprised of Semantic AI and/or native code that's available in a [SKILL](SKILLS.md).
- "The Office SKILL has many FUNCTIONs"
- "The Office SKILL has many FUNCTIONS"
**Native Function** - expressed with traditional computing language (C#, Python, Typescript)
and easily integrates with SK
@@ -9,7 +9,7 @@ interface IData {
runTask: () => Promise<void>;
}
const IteractionButton: FC<IData> = ({ taskDescription, runTask }) => {
const InteractionButton: FC<IData> = ({ taskDescription, runTask }) => {
const [isBusy, setIsBusy] = useState<boolean>(false);
const [canRunTask, setCanRunTask] = useState<boolean>(true);
@@ -37,4 +37,4 @@ const IteractionButton: FC<IData> = ({ taskDescription, runTask }) => {
);
};
export default IteractionButton;
export default InteractionButton;
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,4 @@
# Local Hugging Face Model Inteference Server
# Local Hugging Face Model Inference Server
> [!IMPORTANT]
> This learning sample is for educational purposes only and should not be used in any production
@@ -106,7 +106,7 @@ public static class Example16_CustomLLM
var textValidationFunction = kernel.CreateSemanticFunction(FunctionDefinition);
var result = await textValidationFunction.InvokeAsync("I mised the training sesion this morning");
var result = await textValidationFunction.InvokeAsync("I mised the training session this morning");
Console.WriteLine(result);
// Details of the my custom model response
@@ -121,7 +121,7 @@ public static class Example16_CustomLLM
Console.WriteLine("======== Custom LLM - Text Completion - Raw ========");
var completionService = new MyTextCompletionService();
var result = await completionService.CompleteAsync("I missed the training sesion this morning", new CompleteRequestSettings());
var result = await completionService.CompleteAsync("I missed the training session this morning", new CompleteRequestSettings());
Console.WriteLine(result);
}
@@ -1,403 +1,403 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "68e1c158",
"metadata": {},
"source": [
"# Building Semantic Memory with Embeddings\n",
"\n",
"So far, we've mostly been treating the kernel as a stateless orchestration engine.\n",
"We send text into a model API and receive text out. \n",
"\n",
"In a [previous notebook](04-context-variables-chat.ipynb), we used `context variables` to pass in additional\n",
"text into prompts to enrich them with more context. This allowed us to create a basic chat experience. \n",
"\n",
"However, if you solely relied on context variables, you would quickly realize that eventually your prompt\n",
"would grow so large that you would run into a the model's token limit. What we need is a way to persist state\n",
"and build both short-term and long-term memory to empower even more intelligent applications. \n",
"\n",
"To do this, we dive into the key concept of `Semantic Memory` in the Semantic Kernel. "
]
"cells": [
{
"cell_type": "markdown",
"id": "68e1c158",
"metadata": {},
"source": [
"# Building Semantic Memory with Embeddings\n",
"\n",
"So far, we've mostly been treating the kernel as a stateless orchestration engine.\n",
"We send text into a model API and receive text out. \n",
"\n",
"In a [previous notebook](04-context-variables-chat.ipynb), we used `context variables` to pass in additional\n",
"text into prompts to enrich them with more context. This allowed us to create a basic chat experience. \n",
"\n",
"However, if you solely relied on context variables, you would quickly realize that eventually your prompt\n",
"would grow so large that you would run into a the model's token limit. What we need is a way to persist state\n",
"and build both short-term and long-term memory to empower even more intelligent applications. \n",
"\n",
"To do this, we dive into the key concept of `Semantic Memory` in the Semantic Kernel. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a77bdf89",
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.2.7.dev0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508ad44f",
"metadata": {},
"outputs": [],
"source": [
"from typing import Tuple\n",
"\n",
"import semantic_kernel as sk\n",
"from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion, OpenAITextEmbedding"
]
},
{
"cell_type": "markdown",
"id": "d8ddffc1",
"metadata": {},
"source": [
"In order to use memory, we need to instantiate the Kernel with a Memory Storage\n",
"and an Embedding service. In this example, we make use of the `VolatileMemoryStore` \"which can be thought of as a temporary in-memory storage (not to be confused with Semantic Memory). This memory is not written to disk and is only available during the app session.\n",
"\n",
"When developing your app you will have the option to plug in persistent storage like Azure Cosmos Db, PostgreSQL, SQLite, etc. Semantic Memory allows also to index external data sources, without duplicating all the information, more on that later."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f8dcbc6",
"metadata": {},
"outputs": [],
"source": [
"kernel = sk.Kernel()\n",
"\n",
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
"kernel.add_text_completion_service(\"dv\", OpenAITextCompletion(\"text-davinci-003\", api_key, org_id))\n",
"kernel.add_text_embedding_generation_service(\"ada\", OpenAITextEmbedding(\"text-embedding-ada-002\", api_key, org_id))\n",
"\n",
"kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n",
"kernel.import_skill(sk.core_skills.TextMemorySkill())"
]
},
{
"cell_type": "markdown",
"id": "e7fefb6a",
"metadata": {},
"source": [
"At its core, Semantic Memory is a set of data structures that allow you to store the meaning of text that come from different data sources, and optionally to store the source text too. These texts can be from the web, e-mail providers, chats, a database, or from your local directory, and are hooked up to the Semantic Kernel through data source connectors.\n",
"\n",
"The texts are embedded or compressed into a vector of floats representing mathematically the texts' contents and meaning. You can read more about embeddings [here](https://aka.ms/sk/embeddings)."
]
},
{
"cell_type": "markdown",
"id": "2a7e7ca4",
"metadata": {},
"source": [
"### Manually adding memories\n",
"Let's create some initial memories \"About Me\". We can add memories to our `VolatileMemoryStore` by using `SaveInformationAsync`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d096504c",
"metadata": {},
"outputs": [],
"source": [
"async def populate_memory(kernel: sk.Kernel) -> None:\n",
" # Add some documents to the semantic memory\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info1\", text=\"My name is Andrea\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info2\", text=\"I currently work as a tour guide\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info3\", text=\"I've been living in Seattle since 2005\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info4\", text=\"I visited France and Italy five times since 2015\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info5\", text=\"My family is from New York\"\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "2calf857",
"metadata": {},
"source": [
"Let's try searching the memory:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "628c843e",
"metadata": {},
"outputs": [],
"source": [
"async def search_memory_examples(kernel: sk.Kernel) -> None:\n",
" questions = [\n",
" \"what's my name\",\n",
" \"where do I live?\",\n",
" \"where's my family from?\",\n",
" \"where have I traveled?\",\n",
" \"what do I do for work\",\n",
" ]\n",
"\n",
" for question in questions:\n",
" print(f\"Question: {question}\")\n",
" result = await kernel.memory.search_async(\"aboutMe\", question)\n",
" print(f\"Answer: {result[0].text}\\n\")"
]
},
{
"cell_type": "markdown",
"id": "e70c2b22",
"metadata": {},
"source": [
"Let's now revisit the our chat sample from the [previous notebook](04-context-variables-chat.ipynb).\n",
"If you remember, we used context variables to fill the prompt with a `history` that continuously got populated as we chatted with the bot. Let's add also memory to it!"
]
},
{
"cell_type": "markdown",
"id": "1ed54a32",
"metadata": {},
"source": [
"This is done by using the `TextMemorySkill` which exposes the `recall` native function.\n",
"\n",
"`recall` takes an input ask and performs a similarity search on the contents that have\n",
"been embedded in the Memory Store and returns the most relevant memory. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb8549b2",
"metadata": {},
"outputs": [],
"source": [
"async def setup_chat_with_memory(\n",
" kernel: sk.Kernel,\n",
") -> Tuple[sk.SKFunctionBase, sk.SKContext]:\n",
" sk_prompt = \"\"\"\n",
" ChatBot can have a conversation with you about any topic.\n",
" It can give explicit instructions or say 'I don't know' if\n",
" it does not have an answer.\n",
"\n",
" Information about me, from previous conversations:\n",
" - {{$fact1}} {{recall $fact1}}\n",
" - {{$fact2}} {{recall $fact2}}\n",
" - {{$fact3}} {{recall $fact3}}\n",
" - {{$fact4}} {{recall $fact4}}\n",
" - {{$fact5}} {{recall $fact5}}\n",
"\n",
" Chat:\n",
" {{$chat_history}}\n",
" User: {{$user_input}}\n",
" ChatBot: \"\"\".strip()\n",
"\n",
" chat_func = kernel.create_semantic_function(sk_prompt, max_tokens=200, temperature=0.8)\n",
"\n",
" context = kernel.create_new_context()\n",
" context[\"fact1\"] = \"what is my name?\"\n",
" context[\"fact2\"] = \"where do I live?\"\n",
" context[\"fact3\"] = \"where's my family from?\"\n",
" context[\"fact4\"] = \"where have I traveled?\"\n",
" context[\"fact5\"] = \"what do I do for work?\"\n",
"\n",
" context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"aboutMe\"\n",
" context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = 0.8\n",
"\n",
" context[\"chat_history\"] = \"\"\n",
"\n",
" return chat_func, context"
]
},
{
"cell_type": "markdown",
"id": "1ac62457",
"metadata": {},
"source": [
"The `RelevanceParam` is used in memory search and is a measure of the relevance score from 0.0 to 1.0, where 1.0 means a perfect match. We encourage users to experiment with different values."
]
},
{
"cell_type": "markdown",
"id": "645b55a1",
"metadata": {},
"source": [
"Now that we've included our memories, let's chat!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75267a2f",
"metadata": {},
"outputs": [],
"source": [
"async def chat(\n",
" kernel: sk.Kernel, chat_func: sk.SKFunctionBase, context: sk.SKContext\n",
") -> bool:\n",
" try:\n",
" user_input = input(\"User:> \")\n",
" context[\"user_input\"] = user_input\n",
" print(f\"User:> {user_input}\")\n",
" except KeyboardInterrupt:\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
" except EOFError:\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
"\n",
" if user_input == \"exit\":\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
"\n",
" answer = await kernel.run_async(chat_func, input_vars=context.variables)\n",
" context[\"chat_history\"] += f\"\\nUser:> {user_input}\\nChatBot:> {answer}\\n\"\n",
"\n",
" print(f\"ChatBot:> {answer}\")\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3875a34",
"metadata": {},
"outputs": [],
"source": [
"print(\"Populating memory...\")\n",
"await populate_memory(kernel)\n",
"\n",
"print(\"Asking questions... (manually)\")\n",
"await search_memory_examples(kernel)\n",
"\n",
"print(\"Setting up a chat (with memory!)\")\n",
"chat_func, context = await setup_chat_with_memory(kernel)\n",
"\n",
"print(\"Begin chatting (type 'exit' to exit):\\n\")\n",
"chatting = True\n",
"while chatting:\n",
" chatting = await chat(kernel, chat_func, context)"
]
},
{
"cell_type": "markdown",
"id": "0a51542b",
"metadata": {},
"source": [
"### Adding documents to your memory\n",
"\n",
"Many times in your applications you'll want to bring in external documents into your memory. Let's see how we can do this using our VolatileMemoryStore.\n",
"\n",
"Let's first get some data using some of the links in the Semantic Kernel repo."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3d5a1b9",
"metadata": {},
"outputs": [],
"source": [
"github_files ={}\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/README.md\"] = \\\n",
" \"README: Installation, getting started, and how to contribute\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb\"] = \\\n",
" \"Jupyter notebook describing how to pass prompts from a file to a semantic skill or function\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb\"] = \\\n",
" \"Jupyter notebook describing how to get started with the Semantic Kernel\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT\"] = \\\n",
" \"Sample demonstrating how to create a chat skill interfacing with ChatGPT\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/Volatile/VolatileMemoryStore.cs\"] = \\\n",
" \"C# class that defines a volatile embedding store\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/dotnet/KernelHttpServer/README.md\"] = \\\n",
" \"README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/apps/chat-summary-webapp-react/README.md\"] = \\\n",
" \"README: README associated with a sample starter react-based chat summary webapp\""
]
},
{
"cell_type": "markdown",
"id": "75f3ea5e",
"metadata": {},
"source": [
"Now let's add these files to our VolatileMemoryStore using `SaveReferenceAsync`. We'll separate these memories from the chat memories by putting them in a different collection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "170e7142",
"metadata": {},
"outputs": [],
"source": [
"memory_collection_name = \"SKGitHub\"\n",
"print(\"Adding some GitHub file URLs and their descriptions to a volatile Semantic Memory.\");\n",
"i = 0\n",
"for entry, value in github_files.items():\n",
" await kernel.memory.save_reference_async(\n",
" collection=memory_collection_name,\n",
" description=value,\n",
" text=value,\n",
" external_id=entry,\n",
" external_source_name=\"GitHub\"\n",
" )\n",
" i += 1\n",
" print(\" URL {} saved\".format(i))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "143911c3",
"metadata": {},
"outputs": [],
"source": [
"ask = \"I love Jupyter notebooks, how should I get started?\"\n",
"print(\"===========================\\n\" + \"Query: \" + ask + \"\\n\")\n",
"\n",
"memories = await kernel.memory.search_async(memory_collection_name, ask, limit=5, min_relevance_score=0.77)\n",
"\n",
"i = 0\n",
"for memory in memories:\n",
" i += 1\n",
" print(f\"Result {i}:\")\n",
" print(\" URL: : \" + memory.id)\n",
" print(\" Title : \" + memory.description)\n",
" print(\" Relevance: \" + str(memory.relevance))\n",
" print()"
]
},
{
"cell_type": "markdown",
"id": "59294dac",
"metadata": {},
"source": [
"Now you might be wondering what happens if you have so much data that it doesn't fit into your RAM? That's where you want to make use of an external Vector Database made specifically for storing and retrieving embeddings.\n",
"\n",
"Stay tuned for that!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
{
"cell_type": "code",
"execution_count": null,
"id": "a77bdf89",
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.2.7.dev0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508ad44f",
"metadata": {},
"outputs": [],
"source": [
"from typing import Tuple\n",
"\n",
"import semantic_kernel as sk\n",
"from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion, OpenAITextEmbedding"
]
},
{
"cell_type": "markdown",
"id": "d8ddffc1",
"metadata": {},
"source": [
"In order to use memory, we need to instantiate the Kernel with a Memory Storage\n",
"and an Embedding service. In this example, we make use of the `VolatileMemoryStore` \"which can be thought of as a temporary in-memory storage (not to be confused with Semantic Memory). This memory is not written to disk and is only available during the app session.\n",
"\n",
"When developing your app you will have the option to plug in persistent storage like Azure Cosmos Db, PostgreSQL, SQLite, etc. Semantic Memory allows also to index external data sources, without duplicating all the information, more on that later."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f8dcbc6",
"metadata": {},
"outputs": [],
"source": [
"kernel = sk.Kernel()\n",
"\n",
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
"kernel.add_text_completion_service(\"dv\", OpenAITextCompletion(\"text-davinci-003\", api_key, org_id))\n",
"kernel.add_text_embedding_generation_service(\"ada\", OpenAITextEmbedding(\"text-embedding-ada-002\", api_key, org_id))\n",
"\n",
"kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n",
"kernel.import_skill(sk.core_skills.TextMemorySkill())"
]
},
{
"cell_type": "markdown",
"id": "e7fefb6a",
"metadata": {},
"source": [
"At its core, Semantic Memory is a set of data structures that allow you to store the meaning of text that come from different data sources, and optionally to store the source text too. These texts can be from the web, e-mail providers, chats, a database, or from your local directory, and are hooked up to the Semantic Kernel through data source connectors.\n",
"\n",
"The texts are embedded or compressed into a vector of floats representing mathematically the texts' contents and meaning. You can read more about embeddings [here](https://aka.ms/sk/embeddings)."
]
},
{
"cell_type": "markdown",
"id": "2a7e7ca4",
"metadata": {},
"source": [
"### Manually adding memories\n",
"Let's create some initial memories \"About Me\". We can add memories to our `VolatileMemoryStore` by using `SaveInformationAsync`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d096504c",
"metadata": {},
"outputs": [],
"source": [
"async def populate_memory(kernel: sk.Kernel) -> None:\n",
" # Add some documents to the semantic memory\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info1\", text=\"My name is Andrea\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info2\", text=\"I currently work as a tour guide\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info3\", text=\"I've been living in Seattle since 2005\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info4\", text=\"I visited France and Italy five times since 2015\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info5\", text=\"My family is from New York\"\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "2caf8575",
"metadata": {},
"source": [
"Let's try searching the memory:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "628c843e",
"metadata": {},
"outputs": [],
"source": [
"async def search_memory_examples(kernel: sk.Kernel) -> None:\n",
" questions = [\n",
" \"what's my name\",\n",
" \"where do I live?\",\n",
" \"where's my family from?\",\n",
" \"where have I traveled?\",\n",
" \"what do I do for work\",\n",
" ]\n",
"\n",
" for question in questions:\n",
" print(f\"Question: {question}\")\n",
" result = await kernel.memory.search_async(\"aboutMe\", question)\n",
" print(f\"Answer: {result[0].text}\\n\")"
]
},
{
"cell_type": "markdown",
"id": "e70c2b22",
"metadata": {},
"source": [
"Let's now revisit the our chat sample from the [previous notebook](04-context-variables-chat.ipynb).\n",
"If you remember, we used context variables to fill the prompt with a `history` that continuously got populated as we chatted with the bot. Let's add also memory to it!"
]
},
{
"cell_type": "markdown",
"id": "1ed54a32",
"metadata": {},
"source": [
"This is done by using the `TextMemorySkill` which exposes the `recall` native function.\n",
"\n",
"`recall` takes an input ask and performs a similarity search on the contents that have\n",
"been embedded in the Memory Store and returns the most relevant memory. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb8549b2",
"metadata": {},
"outputs": [],
"source": [
"async def setup_chat_with_memory(\n",
" kernel: sk.Kernel,\n",
") -> Tuple[sk.SKFunctionBase, sk.SKContext]:\n",
" sk_prompt = \"\"\"\n",
" ChatBot can have a conversation with you about any topic.\n",
" It can give explicit instructions or say 'I don't know' if\n",
" it does not have an answer.\n",
"\n",
" Information about me, from previous conversations:\n",
" - {{$fact1}} {{recall $fact1}}\n",
" - {{$fact2}} {{recall $fact2}}\n",
" - {{$fact3}} {{recall $fact3}}\n",
" - {{$fact4}} {{recall $fact4}}\n",
" - {{$fact5}} {{recall $fact5}}\n",
"\n",
" Chat:\n",
" {{$chat_history}}\n",
" User: {{$user_input}}\n",
" ChatBot: \"\"\".strip()\n",
"\n",
" chat_func = kernel.create_semantic_function(sk_prompt, max_tokens=200, temperature=0.8)\n",
"\n",
" context = kernel.create_new_context()\n",
" context[\"fact1\"] = \"what is my name?\"\n",
" context[\"fact2\"] = \"where do I live?\"\n",
" context[\"fact3\"] = \"where's my family from?\"\n",
" context[\"fact4\"] = \"where have I traveled?\"\n",
" context[\"fact5\"] = \"what do I do for work?\"\n",
"\n",
" context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"aboutMe\"\n",
" context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = 0.8\n",
"\n",
" context[\"chat_history\"] = \"\"\n",
"\n",
" return chat_func, context"
]
},
{
"cell_type": "markdown",
"id": "1ac62457",
"metadata": {},
"source": [
"The `RelevanceParam` is used in memory search and is a measure of the relevance score from 0.0 to 1.0, where 1.0 means a perfect match. We encourage users to experiment with different values."
]
},
{
"cell_type": "markdown",
"id": "645b55a1",
"metadata": {},
"source": [
"Now that we've included our memories, let's chat!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75267a2f",
"metadata": {},
"outputs": [],
"source": [
"async def chat(\n",
" kernel: sk.Kernel, chat_func: sk.SKFunctionBase, context: sk.SKContext\n",
") -> bool:\n",
" try:\n",
" user_input = input(\"User:> \")\n",
" context[\"user_input\"] = user_input\n",
" print(f\"User:> {user_input}\")\n",
" except KeyboardInterrupt:\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
" except EOFError:\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
"\n",
" if user_input == \"exit\":\n",
" print(\"\\n\\nExiting chat...\")\n",
" return False\n",
"\n",
" answer = await kernel.run_async(chat_func, input_vars=context.variables)\n",
" context[\"chat_history\"] += f\"\\nUser:> {user_input}\\nChatBot:> {answer}\\n\"\n",
"\n",
" print(f\"ChatBot:> {answer}\")\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3875a34",
"metadata": {},
"outputs": [],
"source": [
"print(\"Populating memory...\")\n",
"await populate_memory(kernel)\n",
"\n",
"print(\"Asking questions... (manually)\")\n",
"await search_memory_examples(kernel)\n",
"\n",
"print(\"Setting up a chat (with memory!)\")\n",
"chat_func, context = await setup_chat_with_memory(kernel)\n",
"\n",
"print(\"Begin chatting (type 'exit' to exit):\\n\")\n",
"chatting = True\n",
"while chatting:\n",
" chatting = await chat(kernel, chat_func, context)"
]
},
{
"cell_type": "markdown",
"id": "0a51542b",
"metadata": {},
"source": [
"### Adding documents to your memory\n",
"\n",
"Many times in your applications you'll want to bring in external documents into your memory. Let's see how we can do this using our VolatileMemoryStore.\n",
"\n",
"Let's first get some data using some of the links in the Semantic Kernel repo."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3d5a1b9",
"metadata": {},
"outputs": [],
"source": [
"github_files ={}\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/README.md\"] = \\\n",
" \"README: Installation, getting started, and how to contribute\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb\"] = \\\n",
" \"Jupyter notebook describing how to pass prompts from a file to a semantic skill or function\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb\"] = \\\n",
" \"Jupyter notebook describing how to get started with the Semantic Kernel\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT\"] = \\\n",
" \"Sample demonstrating how to create a chat skill interfacing with ChatGPT\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/Volatile/VolatileMemoryStore.cs\"] = \\\n",
" \"C# class that defines a volatile embedding store\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/dotnet/KernelHttpServer/README.md\"] = \\\n",
" \"README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4\"\n",
"github_files[\"https://github.com/microsoft/semantic-kernel/tree/main/samples/apps/chat-summary-webapp-react/README.md\"] = \\\n",
" \"README: README associated with a sample starter react-based chat summary webapp\""
]
},
{
"cell_type": "markdown",
"id": "75f3ea5e",
"metadata": {},
"source": [
"Now let's add these files to our VolatileMemoryStore using `SaveReferenceAsync`. We'll separate these memories from the chat memories by putting them in a different collection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "170e7142",
"metadata": {},
"outputs": [],
"source": [
"memory_collection_name = \"SKGitHub\"\n",
"print(\"Adding some GitHub file URLs and their descriptions to a volatile Semantic Memory.\");\n",
"i = 0\n",
"for entry, value in github_files.items():\n",
" await kernel.memory.save_reference_async(\n",
" collection=memory_collection_name,\n",
" description=value,\n",
" text=value,\n",
" external_id=entry,\n",
" external_source_name=\"GitHub\"\n",
" )\n",
" i += 1\n",
" print(\" URL {} saved\".format(i))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "143911c3",
"metadata": {},
"outputs": [],
"source": [
"ask = \"I love Jupyter notebooks, how should I get started?\"\n",
"print(\"===========================\\n\" + \"Query: \" + ask + \"\\n\")\n",
"\n",
"memories = await kernel.memory.search_async(memory_collection_name, ask, limit=5, min_relevance_score=0.77)\n",
"\n",
"i = 0\n",
"for memory in memories:\n",
" i += 1\n",
" print(f\"Result {i}:\")\n",
" print(\" URL: : \" + memory.id)\n",
" print(\" Title : \" + memory.description)\n",
" print(\" Relevance: \" + str(memory.relevance))\n",
" print()"
]
},
{
"cell_type": "markdown",
"id": "59294dac",
"metadata": {},
"source": [
"Now you might be wondering what happens if you have so much data that it doesn't fit into your RAM? That's where you want to make use of an external Vector Database made specifically for storing and retrieving embeddings.\n",
"\n",
"Stay tuned for that!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat": 4,
"nbformat_minor": 5
}
@@ -1,179 +1,179 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "68e1c158",
"metadata": {},
"source": [
"# Using Hugging Face With Skills\n",
"\n",
"In this notebook, we demonstrate using Hugging Face models for Skills using both SemanticMemory and text completions. \n",
"\n",
"SK supports downloading models from the Hugging Face that can perform the following tasks: text-generation, text2text-generation, summarization, and sentence-similarity. You can search for models by task at https://huggingface.co/models."
]
"cells": [
{
"cell_type": "markdown",
"id": "68e1c158",
"metadata": {},
"source": [
"# Using Hugging Face With Skills\n",
"\n",
"In this notebook, we demonstrate using Hugging Face models for Skills using both SemanticMemory and text completions. \n",
"\n",
"SK supports downloading models from the Hugging Face that can perform the following tasks: text-generation, text2text-generation, summarization, and sentence-similarity. You can search for models by task at https://huggingface.co/models."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a77bdf89",
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.2.7.dev0\n",
"\n",
"# Note that additional dependencies are required for the Hugging Face connectors:\n",
"!python -m pip install torch==2.0.0\n",
"!python -m pip install transformers==4.28.1\n",
"!python -m pip install sentence-transformers==2.2.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508ad44f",
"metadata": {},
"outputs": [],
"source": [
"import semantic_kernel as sk\n",
"import semantic_kernel.connectors.ai.hugging_face as sk_hf"
]
},
{
"cell_type": "markdown",
"id": "d8ddffc1",
"metadata": {},
"source": [
"First, we will create a kernel and add both text completion and embedding services. \n",
"\n",
"For text completion, we are choosing GPT2. This is a text-generation model. (Note: text-generation will repeat the input in the output, text2text-generation will not.)\n",
"For embeddings, we are using sentence-transformers/all-MiniLM-L6-v2. Vectors generated for this model are of length 384 (compared to a length of 1536 from OpenAI ADA).\n",
"\n",
"The following step may take a few minutes when run for the first time as the models will be downloaded to your local machine."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f8dcbc6",
"metadata": {},
"outputs": [],
"source": [
"kernel = sk.Kernel()\n",
"\n",
"# Configure LLM service\n",
"kernel.add_text_completion_service(\n",
" \"gpt2\", sk_hf.HuggingFaceTextCompletion(\"gpt2\", task=\"text-generation\")\n",
")\n",
"kernel.add_text_embedding_generation_service(\n",
" \"sentence-transformers/all-MiniLM-L6-v2\",\n",
" sk_hf.HuggingFaceTextEmbedding(\"sentence-transformers/all-MiniLM-L6-v2\"),\n",
")\n",
"kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n",
"kernel.import_skill(sk.core_skills.TextMemorySkill())"
]
},
{
"cell_type": "markdown",
"id": "2a7e7ca4",
"metadata": {},
"source": [
"### Add Memories and Define a skill to use them\n",
"\n",
"Most models available on huggingface.co are not as powerful as OpenAI GPT-3+. Your skills will likely need to be simpler to accommodate this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d096504c",
"metadata": {},
"outputs": [],
"source": [
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info1\", text=\"Sharks are fish.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info2\", text=\"Whales are mammals.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info3\", text=\"Penguins are birds.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info4\", text=\"Dolphins are mammals.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info5\", text=\"Flies are insects.\"\n",
")\n",
"\n",
"# Define semantic function using SK prompt template language\n",
"my_prompt = \"\"\"I know these animal facts: {{recall $query1}} {{recall $query2}} {{recall $query3}} and \"\"\"\n",
"\n",
"# Create the semantic function\n",
"my_function = kernel.create_semantic_function(\n",
" my_prompt, max_tokens=45, temperature=0.5, top_p=0.5\n",
")\n"
]
},
{
"cell_type": "markdown",
"id": "2calf857",
"metadata": {},
"source": [
"Let's now see what the completion looks like! Remember, \"gpt2\" is nowhere near as large as ChatGPT, so expect a much simpler answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "628c843e",
"metadata": {},
"outputs": [],
"source": [
"context = kernel.create_new_context()\n",
"context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"animal-facts\"\n",
"context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = 0.3\n",
"\n",
"context[\"query1\"] = \"animal that swims\"\n",
"context[\"query2\"] = \"animal that flies\"\n",
"context[\"query3\"] = \"penguins are?\"\n",
"output = await kernel.run_async(my_function, input_vars=context.variables)\n",
"\n",
"output = str(output).strip()\n",
"\n",
"\n",
"query_result1 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query1\"], limit=1, min_relevance_score=0.3\n",
")\n",
"query_result2 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query2\"], limit=1, min_relevance_score=0.3\n",
")\n",
"query_result3 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query3\"], limit=1, min_relevance_score=0.3\n",
")\n",
"\n",
"print(f\"gpt2 completed prompt with: '{output}'\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
{
"cell_type": "code",
"execution_count": null,
"id": "a77bdf89",
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.2.7.dev0\n",
"\n",
"# Note that additional dependencies are required for the Hugging Face connectors:\n",
"!python -m pip install torch==2.0.0\n",
"!python -m pip install transformers==4.28.1\n",
"!python -m pip install sentence-transformers==2.2.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508ad44f",
"metadata": {},
"outputs": [],
"source": [
"import semantic_kernel as sk\n",
"import semantic_kernel.connectors.ai.hugging_face as sk_hf"
]
},
{
"cell_type": "markdown",
"id": "d8ddffc1",
"metadata": {},
"source": [
"First, we will create a kernel and add both text completion and embedding services. \n",
"\n",
"For text completion, we are choosing GPT2. This is a text-generation model. (Note: text-generation will repeat the input in the output, text2text-generation will not.)\n",
"For embeddings, we are using sentence-transformers/all-MiniLM-L6-v2. Vectors generated for this model are of length 384 (compared to a length of 1536 from OpenAI ADA).\n",
"\n",
"The following step may take a few minutes when run for the first time as the models will be downloaded to your local machine."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f8dcbc6",
"metadata": {},
"outputs": [],
"source": [
"kernel = sk.Kernel()\n",
"\n",
"# Configure LLM service\n",
"kernel.add_text_completion_service(\n",
" \"gpt2\", sk_hf.HuggingFaceTextCompletion(\"gpt2\", task=\"text-generation\")\n",
")\n",
"kernel.add_text_embedding_generation_service(\n",
" \"sentence-transformers/all-MiniLM-L6-v2\",\n",
" sk_hf.HuggingFaceTextEmbedding(\"sentence-transformers/all-MiniLM-L6-v2\"),\n",
")\n",
"kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n",
"kernel.import_skill(sk.core_skills.TextMemorySkill())"
]
},
{
"cell_type": "markdown",
"id": "2a7e7ca4",
"metadata": {},
"source": [
"### Add Memories and Define a skill to use them\n",
"\n",
"Most models available on huggingface.co are not as powerful as OpenAI GPT-3+. Your skills will likely need to be simpler to accommodate this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d096504c",
"metadata": {},
"outputs": [],
"source": [
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info1\", text=\"Sharks are fish.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info2\", text=\"Whales are mammals.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info3\", text=\"Penguins are birds.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info4\", text=\"Dolphins are mammals.\"\n",
")\n",
"await kernel.memory.save_information_async(\n",
" \"animal-facts\", id=\"info5\", text=\"Flies are insects.\"\n",
")\n",
"\n",
"# Define semantic function using SK prompt template language\n",
"my_prompt = \"\"\"I know these animal facts: {{recall $query1}} {{recall $query2}} {{recall $query3}} and \"\"\"\n",
"\n",
"# Create the semantic function\n",
"my_function = kernel.create_semantic_function(\n",
" my_prompt, max_tokens=45, temperature=0.5, top_p=0.5\n",
")\n"
]
},
{
"cell_type": "markdown",
"id": "2caf8575",
"metadata": {},
"source": [
"Let's now see what the completion looks like! Remember, \"gpt2\" is nowhere near as large as ChatGPT, so expect a much simpler answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "628c843e",
"metadata": {},
"outputs": [],
"source": [
"context = kernel.create_new_context()\n",
"context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"animal-facts\"\n",
"context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = 0.3\n",
"\n",
"context[\"query1\"] = \"animal that swims\"\n",
"context[\"query2\"] = \"animal that flies\"\n",
"context[\"query3\"] = \"penguins are?\"\n",
"output = await kernel.run_async(my_function, input_vars=context.variables)\n",
"\n",
"output = str(output).strip()\n",
"\n",
"\n",
"query_result1 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query1\"], limit=1, min_relevance_score=0.3\n",
")\n",
"query_result2 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query2\"], limit=1, min_relevance_score=0.3\n",
")\n",
"query_result3 = await kernel.memory.search_async(\n",
" \"animal-facts\", context[\"query3\"], limit=1, min_relevance_score=0.3\n",
")\n",
"\n",
"print(f\"gpt2 completed prompt with: '{output}'\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat": 4,
"nbformat_minor": 5
}