* Create cohere-webrequests.ipynb

* Create rerank-cohere-client.ipynb

* Rename cohere-webrequests.ipynb to rerank-webrequests.ipynb

* Create rerank-3-langchain.ipynb

* Update rerank-webrequests.ipynb

* Update rerank-webrequests.ipynb

* Update rerank-webrequests.ipynb

* Update rerank-webrequests.ipynb

* reformatted
This commit is contained in:
shubhirajMsft 2024-07-25 08:20:23 -07:00 коммит произвёл GitHub
Родитель 0d02248521
Коммит 0f676621ca
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 623 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,322 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "xkYGnQdObi10"
},
"source": [
"# Cohere and Azure with LangChain\n",
"\n",
"The following notebook gives examples of how to call Cohere's models deployed on Azure with LangChain"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FuX-V6VkcFNz"
},
"source": [
"First you will need to install the Cohere's package in LangChain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ULXOMqh3bH1o"
},
"outputs": [],
"source": [
"# %pip install langchain_cohere"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nC1szd-NdSVe"
},
"source": [
"Then to set up the Cohere client you will need to input the Azure endpoint URL and token."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "eYR8fTdscQsg"
},
"outputs": [],
"source": [
"import cohere\n",
"import getpass\n",
"from langchain_cohere import ChatCohere\n",
"from langchain_core.messages import HumanMessage\n",
"import os\n",
"\n",
"os.environ[\"COHERE_API_KEY\"] = getpass.getpass(prompt=\"Azure AD token:\")\n",
"url = getpass.getpass(\n",
" prompt=\"Azure endpoint url this should be without /chat or /embed and with /v1 at the end:\"\n",
")\n",
"\n",
"co = ChatCohere(base_url=url)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "niX5Q144dH9F"
},
"source": [
"## Chat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7MEOTibRdFtj"
},
"outputs": [],
"source": [
"messages = [HumanMessage(content=\"Hello\")]\n",
"response = co.invoke(messages)\n",
"print(response.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-Kj3nQCGdCMk"
},
"outputs": [],
"source": [
"messages = [\n",
" HumanMessage(\n",
" content=\"How can organizations effectively manage and secure their cloud infrastructure?\"\n",
" )\n",
"]\n",
"for chunk in co.stream(messages):\n",
" print(chunk.content, end=\"\", flush=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oxUNxct3eh-u"
},
"source": [
"## Chat with RAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Jq-451LrmaFY"
},
"source": [
"Using LangChain's Wiki Retriever. You will need the wikipedia and langchain packages for this example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DEM2T0lRmpQR"
},
"outputs": [],
"source": [
"%pip install --upgrade --quiet wikipedia\n",
"%pip install --upgrade --quiet langchain"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4R3m46W2eXcn"
},
"outputs": [],
"source": [
"from langchain_core.messages import AIMessage\n",
"from langchain.retrievers import WikipediaRetriever\n",
"from typing import Dict, List, Any\n",
"from langchain_core.runnables import RunnablePassthrough\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = lambda x: [\n",
" HumanMessage(\n",
" x[\"question\"], additional_kwargs={\"documents\": x.get(\"documents\", None)}\n",
" )\n",
"]\n",
"\n",
"rag_x_citations_chain = (\n",
" {\"documents\": WikipediaRetriever(), \"question\": RunnablePassthrough()}\n",
" | RunnablePassthrough()\n",
" | prompt\n",
" | co\n",
")\n",
"response = rag_x_citations_chain.invoke(\"What is Task Decomposition?\")\n",
"print(response.content)\n",
"print(response.additional_kwargs[\"citations\"])\n",
"print(response.additional_kwargs[\"documents\"])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oI-rRnt2-6MA"
},
"source": [
"## Chat with Tools\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f1XyC9AF-2LV"
},
"outputs": [],
"source": [
"%pip install --quiet langchain langchain-core langchain-cohere"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KARcbM2H-0V3"
},
"outputs": [],
"source": [
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
"from langchain_cohere import ChatCohere, create_cohere_tools_agent\n",
"from langchain.agents import AgentExecutor\n",
"from langchain_core.prompts.chat import ChatPromptTemplate\n",
"from langchain_core.messages import HumanMessage\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"import os\n",
"import getpass\n",
"\n",
"os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(prompt=\"Tavily API key:\")\n",
"\n",
"internet_search = TavilySearchResults()\n",
"internet_search.name = \"internet_search\"\n",
"internet_search.description = \"Returns a list of relevant document snippets for a textual query retrieved from the internet.\"\n",
"\n",
"\n",
"class TavilySearchInput(BaseModel):\n",
" query: str = Field(description=\"Query to search the internet with\")\n",
"\n",
"\n",
"internet_search.args_schema = TavilySearchInput\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" HumanMessage(\n",
" content=\"In what year was the company that was founded as Sound of Music added to the S&P 500?\"\n",
" )\n",
" ]\n",
")\n",
"\n",
"llm = co.bind()\n",
"agent = create_cohere_tools_agent(llm, [internet_search], prompt)\n",
"agent_executor = AgentExecutor(agent=agent, tools=[internet_search], verbose=True)\n",
"agent_executor.invoke({})"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b8nJ4X0FdgtR"
},
"source": [
"## Embed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Fo-052HWdKwQ"
},
"outputs": [],
"source": [
"import cohere\n",
"import getpass\n",
"from langchain_cohere import CohereEmbeddings\n",
"from langchain_core.messages import HumanMessage\n",
"import os\n",
"\n",
"# os.environ[\"COHERE_API_KEY\"] = getpass.getpass(prompt=\"Azure AD token:\")\n",
"url = getpass.getpass(\n",
" prompt=\"Azure endpoint url this should be without /chat or /embed and with /v1 at the end:\"\n",
")\n",
"\n",
"co = CohereEmbeddings(base_url=url)\n",
"query_result = co.embed(\n",
" texts=[\"Embed this sentence please\"], input_type=\"classification\"\n",
")\n",
"print(query_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rerank"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cohere\n",
"import getpass\n",
"from langchain_cohere import CohereRerank\n",
"import os\n",
"\n",
"os.environ[\"COHERE_API_KEY\"] = getpass.getpass(prompt=\"Azure AD token:\")\n",
"\n",
"co = CohereRerank(cohere_api_key=os.environ[\"COHERE_API_KEY\"])\n",
"query = \"What is the capital of the United States?\"\n",
"docs = [\n",
" \"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\",\n",
" \"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\",\n",
" \"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\",\n",
" \"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\",\n",
" \"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\",\n",
"]\n",
"results = co.rerank(model=\"rerank-english-v3.0\", query=query, documents=docs, top_n=5)\n",
"results"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.8.19"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

Просмотреть файл

@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Use Cohere Rerank in Azure AI and Azure ML\n",
"\n",
"Use `cohere` client to consume Cohere Rerank model deployments in Azure AI and Azure ML..\n",
"\n",
"> Review the [documentation](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/deploy-models-cohere) for the Cohere family of models at for AI Studio and for ML Studio for details on how to provision inference endpoints, regional availability, pricing and inference schema reference."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prerequisites\n",
"\n",
"Before we start, there are certain steps we need to take to deploy the models:\n",
"\n",
"* Register for a valid Azure account with subscription \n",
"* Make sure you have access to [Azure AI Studio](https://learn.microsoft.com/en-us/azure/ai-studio/what-is-ai-studio?tabs=home)\n",
"* Create a project and resource group\n",
"* Select `Cohere Rerank V3 (English)` or `Cohere Rerank V3 (multilingual)`.\n",
"\n",
" > Notice that some models may not be available in all the regions in Azure AI and Azure Machine Learning. On those cases, you can create a workspace or project in the region where the models are available and then consume it with a connection from a different one. To learn more about using connections see [Consume models with connections](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/deployments-connections)\n",
"\n",
"* Deploy with \"Pay-as-you-go\"\n",
"\n",
"Once deployed successfully, you should be assigned for an API endpoint and a security key for inference.\n",
"\n",
"For more information, you should consult Azure's official documentation [here](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/deploy-models-cohere) for model deployment and inference.\n",
"\n",
"To complete this tutorial, you will need to:\n",
"\n",
"* Install `cohere`:\n",
"\n",
" ```bash\n",
" pip install cohere\n",
" ```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example\n",
"\n",
"The following is an example about how to use `cohere` with a Cohere Rerank model deployed in Azure AI and Azure ML:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"name": "imports"
},
"outputs": [],
"source": [
"import cohere"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use `cohere`, create a client and configure it as follows:\n",
"\n",
"- `endpoint`: Use the endpoint URL from your deployment. Include `/v1` at the end of the endpoint.\n",
"- `api_key`: Use your API key."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"name": "chat_client"
},
"outputs": [],
"source": [
"co = cohere.Client(\n",
" base_url=\"https://<endpoint>.<region>.inference.ai.azure.com/v1\", api_key=\"<key>\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the client to create the requests:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"name": "chat_invoke"
},
"outputs": [],
"source": [
"documents = [\n",
" {\n",
" \"Title\": \"Incorrect Password\",\n",
" \"Content\": \"Hello, I have been trying to access my account for the past hour and it keeps saying my password is incorrect. Can you please help me?\",\n",
" },\n",
" {\n",
" \"Title\": \"Confirmation Email Missed\",\n",
" \"Content\": \"Hi, I recently purchased a product from your website but I never received a confirmation email. Can you please look into this for me?\",\n",
" },\n",
" {\n",
" \"Title\": \"Questions about Return Policy\",\n",
" \"Content\": \"Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.\",\n",
" },\n",
" {\n",
" \"Title\": \"Customer Support is Busy\",\n",
" \"Content\": \"Good morning, I have been trying to reach your customer support team for the past week but I keep getting a busy signal. Can you please help me?\",\n",
" },\n",
" {\n",
" \"Title\": \"Received Wrong Item\",\n",
" \"Content\": \"Hi, I have a question about my recent order. I received the wrong item and I need to return it.\",\n",
" },\n",
" {\n",
" \"Title\": \"Customer Service is Unavailable\",\n",
" \"Content\": \"Hello, I have been trying to reach your customer support team for the past hour but I keep getting a busy signal. Can you please help me?\",\n",
" },\n",
" {\n",
" \"Title\": \"Return Policy for Defective Product\",\n",
" \"Content\": \"Hi, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.\",\n",
" },\n",
" {\n",
" \"Title\": \"Wrong Item Received\",\n",
" \"Content\": \"Good morning, I have a question about my recent order. I received the wrong item and I need to return it.\",\n",
" },\n",
" {\n",
" \"Title\": \"Return Defective Product\",\n",
" \"Content\": \"Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.\",\n",
" },\n",
"]\n",
"\n",
"response = co.rerank(\n",
" documents=documents,\n",
" query=\"What emails have been about returning items?\",\n",
" rank_fields=[\"Title\", \"Content\"],\n",
" top_n=5,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aditional resources\n",
"\n",
"Here are some additional reference: \n",
"\n",
"* [Plan and manage costs (marketplace)](https://learn.microsoft.com/azure/ai-studio/how-to/costs-plan-manage#monitor-costs-for-models-offered-through-the-azure-marketplace)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "jupyter",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Просмотреть файл

@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HTTP Requests API Usage in CLI\n",
"\n",
"\n",
"\n",
"## Basics\n",
"For using the REST API, You will need to have a Endpoint url and Authentication Key associated with that endpoint.\n",
"This can be acquired by following the deployment steps for AI Studio.\n",
"\n",
"In this embedding completion example, we use a simple curl call for illustration. There are three major components:\n",
"\n",
"* The host-url is your endpoint url with embed completion schema /v1/rerank.\n",
"* The headers defines the content type as well as your api key.\n",
"* The payload or data, which is your prompt detail and model hyper parameters."
]
},
{
"source": [
"!curl --request POST \\\n",
" --url https://your-endpoint.inference.ai/azure/com/v1/rerank \\\n",
" --header 'Authorization: Bearer your-auth-key' \\\n",
" --header 'Cohere-Version: 2022-12-06' \\\n",
" --header 'Content-Type: application/json' \\\n",
" --data '{\"query\": \"What is the capital of the United States?\", \"model\":\"rerank-english-v3.0\",\"return_documents\": true, \"documents\" : [\"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\",\"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\",\"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\",\"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\",\"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\"],\"top_n\": 3}'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HTTP Requests API Usage in Python\n",
"Besides calling the API directly from command line tools. You can also programatically call them in Python. Here there is an embed example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'{\"id\":\"3750ff1f-a1d8-4824-89e4-3cd7b1eb6447\",\"results\":[{\"document\":{\"text\":\"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\"},\"index\":3,\"relevance_score\":0.9990564},{\"document\":{\"text\":\"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\"},\"index\":4,\"relevance_score\":0.7516481},{\"document\":{\"text\":\"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\"},\"index\":1,\"relevance_score\":0.08882029}],\"meta\":{\"api_version\":{\"version\":\"unspecified\",\"is_deprecated\":true},\"warnings\":[\"Please set an API version, for more information please refer to https://docs.cohere.com/versioning-reference\",\"Version is deprecated, for more information please refer to https://docs.cohere.com/versioning-reference\"],\"billed_units\":{\"search_units\":1}}}'\n"
]
}
],
"source": [
"import urllib.request\n",
"import json\n",
"\n",
"# Configure payload data sending to API endpoint\n",
"data = {\n",
" \"query\": \"What is the capital of the United States?\",\n",
" \"model\": \"rerank-english-v3.0\",\n",
" \"return_documents\": True,\n",
" \"documents\": [\n",
" \"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\",\n",
" \"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\",\n",
" \"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\",\n",
" \"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\",\n",
" \"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\",\n",
" ],\n",
" \"top_n\": 3,\n",
"}\n",
"\n",
"body = str.encode(json.dumps(data))\n",
"\n",
"# Replace the url with your API endpoint\n",
"url = \"https://your-endpoint.inference.ai/azure/com/v1/rerank\"\n",
"\n",
"# Replace this with the key for the endpoint\n",
"api_key = \"bearer <your-api-key>\"\n",
"if not api_key:\n",
" raise Exception(\"API Key is missing\")\n",
"\n",
"headers = {\"Content-Type\": \"application/json\", \"Authorization\": (api_key)}\n",
"\n",
"req = urllib.request.Request(url, body, headers)\n",
"\n",
"try:\n",
" response = urllib.request.urlopen(req)\n",
" result = response.read()\n",
" print(result)\n",
"except urllib.error.HTTPError as error:\n",
" print(\"The request failed with status code: \" + str(error.code))\n",
" # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure\n",
" print(error.info())\n",
" print(error.read().decode(\"utf8\", \"ignore\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}