"Load environment variables. You can use anything you like but I used `python-dotenv`. Just create a `.env` file with your `OPENAI_API_KEY` then load it as follows:"
"/Users/elvissaravia/opt/miniconda3/envs/peguide/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.1.0 and will be removed in 0.2.0. Use langchain_openai.ChatOpenAI instead.\n",
"ChatGPT support different types of messages identifiable by the role. LangChain. Recall how we make a basic call to ChatGPT using `openai`? Here is an example:\n",
"\n",
"```python\n",
"MODEL = \"gpt-3.5-turbo\"\n",
"\n",
"response = openai.ChatCompletion.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are an AI research assistant. You use a tone that is technical and scientific.\"},\n",
" {\"role\": \"user\", \"content\": \"Hello, who are you?\"},\n",
" {\"role\": \"assistant\", \"content\": \"Greeting! I am an AI research assistant. How can I help you today?\"},\n",
" {\"role\": \"user\", \"content\": \"Can you tell me about the creation of black holes?\"}\n",
" ],\n",
" temperature=0,\n",
")\n",
"```\n",
"\n",
"LangChain supports these different types of messages, including a arbitrary role parameter (`ChatMessage`). Let's try: "
" SystemMessage(content=\"You are a helpful assistant that can classify the sentiment of input texts. The labels you can use are positive, negative and neutral.\"),\n",
" HumanMessage(content=\"Classify the following sentence: I am doing brilliant today!\"),\n",
"AIMessage(content=\"Certainly! Black holes are fascinating astronomical objects that form from the remnants of massive stars. The creation of a black hole occurs through a process known as stellar collapse.\\n\\nWhen a massive star exhausts its nuclear fuel, it can no longer sustain the outward pressure generated by nuclear fusion. As a result, the star's core collapses under the force of gravity. This collapse is triggered by the imbalance between the inward gravitational force and the outward pressure.\\n\\nDuring the collapse, the star's core becomes incredibly dense, packing an enormous amount of mass into a tiny volume. This extreme density leads to the formation of a singularity, a point of infinite density at the center of the black hole.\\n\\nSurrounding the singularity is the event horizon, which is the boundary beyond which nothing, not even light, can escape the gravitational pull of the black hole. The event horizon is determined by the mass of the black hole, with larger black holes having larger event horizons.\\n\\nThe formation of black holes is classified into three main types based on their mass: stellar black holes, intermediate-mass black holes, and supermassive black holes. Stellar black holes typically have masses several times that of our Sun, while supermassive black holes can have millions or even billions of times the mass of the Sun.\\n\\nIn addition to stellar collapse, black holes can also form through other mechanisms, such as the collision of neutron stars or the accretion of matter onto an existing black hole.\\n\\nUnderstanding the creation and behavior of black holes is a fascinating area of research in astrophysics, with implications for our understanding of gravity, spacetime, and the evolution of galaxies.\")"
"LLMResult(generations=[[ChatGeneration(text=\"Certainly! Black holes are fascinating astronomical objects that are formed through the gravitational collapse of massive stars. The creation of a black hole occurs when a star exhausts its nuclear fuel and can no longer sustain the outward pressure generated by nuclear fusion in its core.\\n\\nWhen a massive star reaches the end of its life, it undergoes a supernova explosion. During this explosive event, the outer layers of the star are ejected into space, while the core collapses inward due to its own gravity. If the core's mass is above a certain threshold, known as the Tolman-Oppenheimer-Volkoff (TOV) limit, the collapse continues until it forms a black hole.\\n\\nThe core collapse is an incredibly dense and energetic process. As the core collapses, its mass becomes concentrated into an extremely small volume, resulting in a gravitational field so strong that not even light can escape from it. This region of space is known as the event horizon, which defines the boundary of a black hole.\\n\\nThe formation of a black hole is governed by the principles of general relativity, a theory proposed by Albert Einstein. According to general relativity, the collapse of matter creates a singularity at the center of the black hole, where the laws of physics as we currently understand them break down.\\n\\nBlack holes come in different sizes, ranging from stellar-mass black holes, which have masses several times that of our Sun, to supermassive black holes, which can have millions or even billions of times the mass of the Sun. Supermassive black holes are thought to form through a combination of processes, including the accretion of matter and the merging of smaller black holes.\\n\\nStudying black holes is a challenging task due to their elusive nature, but advancements in observational techniques, such as gravitational wave detectors and telescopes, have provided us with valuable insights into their formation and behavior.\", generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content=\"Certainly! Black holes are fascinating astronomical objects that are formed through the gravitational collapse of massive stars. The creation of a black hole occurs when a star exhausts its nuclear fuel and can no longer sustain the outward pressure generated by nuclear fusion in its core.\\n\\nWhen a massive star reaches the end of its life, it undergoes a supernova explosion. During this explosive event, the outer layers of the star are ejected into space, while the core collapses inward due to its own gravity. If the core's mass is above a certain threshold, known as the Tolman-Oppenheimer-Volkoff (TOV) limit, the collapse continues until it forms a black hole.\\n\\nThe core collapse is an incredibly dense and energetic process. As the core collapses, its mass becomes concentrated into an extremely small volume, resulting in a gravitational field so strong that not even light can escape from it. This region of space is known as the event horizon, which defines the boundary of a black hole.\\n\\nThe formation of a black hole is governed by the principles of general relativity, a theory proposed by Albert Einstein. According to general relativity, the collapse of matter creates a singularity at the center of the black hole, where the laws of physics as we currently understand them break down.\\n\\nBlack holes come in different sizes, ranging from stellar-mass black holes, which have masses several times that of our Sun, to supermassive black holes, which can have millions or even billions of times the mass of the Sun. Supermassive black holes are thought to form through a combination of processes, including the accretion of matter and the merging of smaller black holes.\\n\\nStudying black holes is a challenging task due to their elusive nature, but advancements in observational techniques, such as gravitational wave detectors and telescopes, have provided us with valuable insights into their formation and behavior.\"))], [ChatGeneration(text='Certainly! Dark mat
"template = \"You are a helpful assistant that can classify the sentiment of input texts. The labels you can use are {sentiment_labels}. Classify the following sentence:\"\n",
"chat.invoke(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"I am doing brilliant today!\").to_messages())"
"chat.invoke(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"Not sure what the weather is like today.\").to_messages())"