docs: nits (#20577)

This commit is contained in:
Erick Friis 2024-04-17 17:20:44 -07:00 committed by GitHub
parent e7e94b37f1
commit 084bedd16e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 73 additions and 36 deletions

View File

@ -19,59 +19,85 @@
"\n", "\n",
">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n", ">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n",
"\n", "\n",
"This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. We recommend having version `openai>=1` installed." "This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. First, we need to install the `langchain-openai` package."
]
},
{
"cell_type": "raw",
"id": "d83ba7de",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"%pip install -qU langchain-openai"
]
},
{
"cell_type": "markdown",
"id": "e39133c8",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"Next, let's set some environment variables to help us connect to the Azure OpenAI service. You can find these values in the Azure portal."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"id": "96164b42", "id": "1d8d73bd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import os\n", "import os\n",
"\n", "\n",
"from langchain_core.messages import HumanMessage\n", "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
"from langchain_openai import AzureChatOpenAI" "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\"\n",
"os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"2023-06-01-preview\"\n",
"os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"] = \"chat\""
]
},
{
"cell_type": "markdown",
"id": "e7b160f8",
"metadata": {},
"source": [
"Next, let's construct our model and chat with it:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64", "id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n", "from langchain_core.messages import HumanMessage\n",
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\"" "from langchain_openai import AzureChatOpenAI\n",
] "\n",
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8161278f",
"metadata": {},
"outputs": [],
"source": [
"model = AzureChatOpenAI(\n", "model = AzureChatOpenAI(\n",
" openai_api_version=\"2023-05-15\",\n", " openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
" azure_deployment=\"your-deployment-name\",\n", " azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
")" ")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 4,
"id": "99509140", "id": "99509140",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"AIMessage(content=\"J'adore la programmation.\")" "AIMessage(content=\"J'adore programmer.\", response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 19, 'total_tokens': 25}, 'model_name': 'gpt-35-turbo', 'system_fingerprint': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='run-25ed88db-38f2-4b0c-a943-a03f217711a9-0')"
] ]
}, },
"execution_count": 15, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -80,7 +106,7 @@
"message = HumanMessage(\n", "message = HumanMessage(\n",
" content=\"Translate this sentence from English to French. I love programming.\"\n", " content=\"Translate this sentence from English to French. I love programming.\"\n",
")\n", ")\n",
"model([message])" "model.invoke([message])"
] ]
}, },
{ {
@ -96,7 +122,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 5,
"id": "0531798a", "id": "0531798a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -106,19 +132,29 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 7,
"id": "aceddb72", "id": "aceddb72",
"metadata": { "metadata": {
"scrolled": true "scrolled": true
}, },
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Cost (USD): $0.000041\n"
]
}
],
"source": [ "source": [
"model = AzureChatOpenAI(\n", "model = AzureChatOpenAI(\n",
" openai_api_version=\"2023-05-15\",\n", " openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
" azure_deployment=\"gpt-35-turbo\", # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n", " azure_deployment=os.environ[\n",
" \"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"\n",
" ], # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
")\n", ")\n",
"with get_openai_callback() as cb:\n", "with get_openai_callback() as cb:\n",
" model([message])\n", " model.invoke([message])\n",
" print(\n", " print(\n",
" f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n", " f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n",
" ) # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used" " ) # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used"
@ -134,7 +170,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 11,
"id": "8d5e54e9", "id": "8d5e54e9",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -147,13 +183,13 @@
} }
], ],
"source": [ "source": [
"model0613 = AzureChatOpenAI(\n", "model0301 = AzureChatOpenAI(\n",
" openai_api_version=\"2023-05-15\",\n", " openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
" deployment_name=\"gpt-35-turbo\",\n", " azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
" model_version=\"0613\",\n", " model_version=\"0301\",\n",
")\n", ")\n",
"with get_openai_callback() as cb:\n", "with get_openai_callback() as cb:\n",
" model0613([message])\n", " model0301.invoke([message])\n",
" print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")" " print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")"
] ]
} }
@ -174,7 +210,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.12" "version": "3.11.4"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -13,6 +13,7 @@ These providers have standalone `langchain-{provider}` packages for improved ver
- [AI21](/docs/integrations/providers/ai21) - [AI21](/docs/integrations/providers/ai21)
- [Airbyte](/docs/integrations/providers/airbyte) - [Airbyte](/docs/integrations/providers/airbyte)
- [Amazon Web Services](/docs/integrations/platforms/aws)
- [Anthropic](/docs/integrations/platforms/anthropic) - [Anthropic](/docs/integrations/platforms/anthropic)
- [Astra DB](/docs/integrations/providers/astradb) - [Astra DB](/docs/integrations/providers/astradb)
- [Cohere](/docs/integrations/providers/cohere) - [Cohere](/docs/integrations/providers/cohere)