Skip to content

Commit dec8f35

Browse files
author
Zhi Zhou
committed
Add in_context_chatcompletions_example_restapi sample code
1 parent ca5c18c commit dec8f35

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-1
lines changed

Basic_Samples/GPT-4V/config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"OPENAI_API_BASE":"https://<Your Azure Resource Name>.openai.azure.com",
44
"OPENAI_API_VERSION":"<OpenAI API Version>",
55

6-
"VISION_API_ENDPOINT": "https://<Your Azure Vision Resource Name>.cognitiveservices.azure.com"
6+
"VISION_API_ENDPOINT": "https://<Your Azure Vision Resource Name>.cognitiveservices.azure.com",
7+
8+
"AZURE_SEARCH_SERVICE_ENDPOINT": "https://<Your Azure Search Resource Name>.search.windows.net",
9+
"AZURE_SEARCH_INDEX_NAME": "<Your Azure Search Index Name>"
710
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "759f9ec0",
6+
"metadata": {},
7+
"source": [
8+
"<h1 align =\"center\"> REST API In Context Learning Samples</h1>\n",
9+
"<hr>\n",
10+
" \n",
11+
"# Chat Completions"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "f4b3d21a",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"import json\n",
22+
"import os\n",
23+
"import requests\n",
24+
"import base64"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"id": "5b2d4a0f",
30+
"metadata": {},
31+
"source": [
32+
"### Setup Parameters\n",
33+
"\n",
34+
"\n",
35+
"Here we will load the configurations from _config.json_ file to setup search_service_endpoint, search_index_name, search_query_key, deployment_name, openai_api_base, openai_api_key and openai_api_version."
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"id": "fd85fb30",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"# Load config values\n",
46+
"with open(r'config.json') as config_file:\n",
47+
" config_details = json.load(config_file)\n",
48+
"\n",
49+
"# Setting up the Azure Search service endpoint.e.g. https://<your search service name>.search.windows.net \n",
50+
"search_service_endpoint = config_details['AZURE_SEARCH_SERVICE_ENDPOINT']\n",
51+
"\n",
52+
"# Setting up the Azure Search service index\n",
53+
"search_index_name = config_details['AZURE_SEARCH_INDEX_NAME']\n",
54+
"\n",
55+
"# Setting up the Azure Search service query key\n",
56+
"search_query_key = os.getenv(\"AZURE_SEARCH_QUERY_KEY\")\n",
57+
"\n",
58+
"# Setting up the deployment name\n",
59+
"deployment_name = config_details['GPT-4V_MODEL']\n",
60+
"\n",
61+
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
62+
"openai_api_base = config_details['OPENAI_API_BASE']\n",
63+
"\n",
64+
"# The API key for your Azure OpenAI resource.\n",
65+
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
66+
"\n",
67+
"# Currently OPENAI API have the following versions available: 2022-12-01. All versions follow the YYYY-MM-DD date structure.\n",
68+
"openai_api_version = config_details['OPENAI_API_VERSION']"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 4,
74+
"id": "b6165c63",
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"It's a Gala apple.\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"# Create Azure Search index (the link will be updated once it goes public)\n",
87+
"# https://github.com/zhizhoualan/cognitive-search-vector-pr/blob/main/demo-python/code/azure-search-vector-image-index-creation-python-sample.ipynb\n",
88+
"\n",
89+
"# System messages and user prompt\n",
90+
"sys_message = \"You are an AI assistant that helps people find information.\"\n",
91+
"user_prompt = \"What is the type of this apple?\"\n",
92+
"\n",
93+
"# Encode the image in base64\n",
94+
"image_file_path = \"../../common/images/test_Gala.jpeg\" # Update with your image path\n",
95+
"with open(image_file_path, 'rb') as image_file:\n",
96+
" encoded_image = base64.b64encode(image_file.read()).decode('ascii')\n",
97+
" \n",
98+
"# Construct the API request URL\n",
99+
"api_url = f\"{openai_api_base}/openai/deployments/{deployment_name}/extensions/chat/completions?api-version={openai_api_version}\"\n",
100+
"\n",
101+
"# Including the api-key in HTTP headers\n",
102+
"headers = {\n",
103+
" \"Content-Type\": \"application/json\",\n",
104+
" \"api-key\": openai_api_key,\n",
105+
"}\n",
106+
"\n",
107+
"# Payload for the request\n",
108+
"payload = {\n",
109+
" \"dataSources\": [\n",
110+
" {\n",
111+
" \"type\": \"AzureCognitiveSearch\",\n",
112+
" \"parameters\": {\n",
113+
" \"endpoint\": search_service_endpoint,\n",
114+
" \"key\": search_query_key,\n",
115+
" \"indexName\": search_index_name\n",
116+
" }\n",
117+
" }\n",
118+
" ],\n",
119+
" \"messages\": [\n",
120+
" {\n",
121+
" \"role\": \"system\",\n",
122+
" \"content\": [\n",
123+
" sys_message\n",
124+
" ]\n",
125+
" },\n",
126+
" {\n",
127+
" \"role\": \"user\",\n",
128+
" \"content\": [\n",
129+
" user_prompt, # Pass the prompt\n",
130+
" {\n",
131+
" \"image\": encoded_image #Pass the encoded image\n",
132+
" }\n",
133+
" ]\n",
134+
" }\n",
135+
" ],\n",
136+
" \"temperature\": 0.7,\n",
137+
" \"top_p\": 0.95,\n",
138+
" \"max_tokens\": 800\n",
139+
"}\n",
140+
"\n",
141+
"# Send the request and handle the response\n",
142+
"try:\n",
143+
" response = requests.post(api_url, headers=headers, json=payload)\n",
144+
" response.raise_for_status() # Raise an error for bad HTTP status codes\n",
145+
" response_content = response.json()\n",
146+
" print(response_content['choices'][0]['message']['content'])\n",
147+
"except requests.RequestException as e:\n",
148+
" raise SystemExit(f\"Failed to make the request. Error: {e}\")"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": []
157+
}
158+
],
159+
"metadata": {
160+
"kernelspec": {
161+
"display_name": "Python 3 (ipykernel)",
162+
"language": "python",
163+
"name": "python3"
164+
},
165+
"language_info": {
166+
"codemirror_mode": {
167+
"name": "ipython",
168+
"version": 3
169+
},
170+
"file_extension": ".py",
171+
"mimetype": "text/x-python",
172+
"name": "python",
173+
"nbconvert_exporter": "python",
174+
"pygments_lexer": "ipython3",
175+
"version": "3.9.18"
176+
}
177+
},
178+
"nbformat": 4,
179+
"nbformat_minor": 5
180+
}

common/images/test_Gala.jpeg

480 KB
Loading

0 commit comments

Comments
 (0)