mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
185 lines
4.8 KiB
Plaintext
185 lines
4.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f970f757-ec76-4bf0-90cd-a2fb68b945e3",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Exploring OpenAI V1 functionality\n",
|
|
"\n",
|
|
"On 11.06.23 OpenAI released a number of new features, and along with it bumped their Python SDK to 1.0.0. This notebook shows off the new features and how to use them with LangChain."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ee897729-263a-4073-898f-bb4cf01ed829",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install \"openai>=1\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "71c34763-d1e7-4b9a-a9d7-3e4cc0dfc2c4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## [JSON mode](https://platform.openai.com/docs/guides/text-generation/json-mode)\n",
|
|
"\n",
|
|
"Constrain the model to only generate valid JSON. Note that you must include a system message with instructions to use JSON for this mode to work.\n",
|
|
"\n",
|
|
"Only works with certain models. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "c3e067ce-7a43-47a7-bc89-41f1de4cf136",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.chat_models import ChatOpenAI\n",
|
|
"from langchain.schema.messages import HumanMessage, SystemMessage"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "db6072c4-f3f3-415d-872b-71ea9f3c02bb",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{\n",
|
|
" \"companies\": [\n",
|
|
" {\n",
|
|
" \"name\": \"Google\",\n",
|
|
" \"origin\": \"USA\"\n",
|
|
" },\n",
|
|
" {\n",
|
|
" \"name\": \"Deepmind\",\n",
|
|
" \"origin\": \"UK\"\n",
|
|
" }\n",
|
|
" ]\n",
|
|
"}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"chat = ChatOpenAI(model=\"gpt-3.5-turbo-1106\").bind(\n",
|
|
" response_format={\"type\": \"json_object\"}\n",
|
|
")\n",
|
|
"\n",
|
|
"output = chat.invoke(\n",
|
|
" [\n",
|
|
" SystemMessage(\n",
|
|
" content=\"Extract the 'name' and 'origin' of any companies mentioned in the following statement. Return a JSON list.\"\n",
|
|
" ),\n",
|
|
" HumanMessage(\n",
|
|
" content=\"Google was founded in the USA, while Deepmind was founded in the UK\"\n",
|
|
" ),\n",
|
|
" ]\n",
|
|
")\n",
|
|
"print(output.content)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "08e00ccf-b991-4249-846b-9500a0ccbfa0",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'companies': [{'name': 'Google', 'origin': 'USA'},\n",
|
|
" {'name': 'Deepmind', 'origin': 'UK'}]}"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"json.loads(output.content)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "aa9a94d9-4319-4ab7-a979-c475ce6b5f50",
|
|
"metadata": {},
|
|
"source": [
|
|
"## [System fingerprint](https://platform.openai.com/docs/guides/text-generation/reproducible-outputs)\n",
|
|
"\n",
|
|
"OpenAI sometimes changes model configurations in a way that impacts outputs. Whenever this happens, the system_fingerprint associated with a generation will change."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "1281883c-bf8f-4665-89cd-4f33ccde69ab",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'token_usage': {'completion_tokens': 43, 'prompt_tokens': 49, 'total_tokens': 92}, 'model_name': 'gpt-3.5-turbo-1106', 'system_fingerprint': 'fp_eeff13170a'}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"chat = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n",
|
|
"output = chat.generate(\n",
|
|
" [\n",
|
|
" [\n",
|
|
" SystemMessage(\n",
|
|
" content=\"Extract the 'name' and 'origin' of any companies mentioned in the following statement. Return a JSON list.\"\n",
|
|
" ),\n",
|
|
" HumanMessage(\n",
|
|
" content=\"Google was founded in the USA, while Deepmind was founded in the UK\"\n",
|
|
" ),\n",
|
|
" ]\n",
|
|
" ]\n",
|
|
")\n",
|
|
"print(output.llm_output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5c637ba1-322d-4fc9-b97e-3afa83dc4d72",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "poetry-venv",
|
|
"language": "python",
|
|
"name": "poetry-venv"
|
|
},
|
|
"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.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|