Creating an LLM Instance

In the Initializing the LLM lesson of Neo4j & LLM Fundamentals, you learned how to initialize an LLM class and generate a response from an LLM. In this lesson, you will need to put this learning into practice by creating an LLM instance to communicate with a GPT model using OpenAI.

You will need to:

  1. Obtain an API key from platform.openai.com

  2. Create a secrets file to save the API key

  3. Initialize an instance of the ChatOpenAI

  4. Create an instance of the OpenAIEmbeddings model

Open in Online IDE →

Obtain an API Key

To obtain an API key, you will need to log into platform.openai.com. Use the left-hand menu to navigate to the API keys section and click + Create new secret key. Give the key the name GraphAcademy Course and click Create secret key.

If you have successfully followed these steps, you will be presented with a new API key. Click the Copy icon to the right of the key to copy to your clipboard.

Setting Streamlit Secrets

The Streamlit documentation outlines four approaches to handling secrets and credentials in your application. For simplicity, we recommend creating a secrets.toml file in .streamlit/ folder.

Copy the following text into a newly created .streamlit/secrets.toml file, replacing the value in quotes with your API key.

Below the API key, add another entry called OPENAI_MODEL. We have found that using gpt-4 yields the best results, but you can also select a different model if you are concerned about costs.

toml
.streamlit/secrets.toml
OPENAI_API_KEY = "sk-..."
OPENAI_MODEL = "gpt-3.5-turbo"

You can access this value in your Streamlit application using the st.secrets map.

python
Accessing Secrets
import streamlit as st

openai_api_key = st.secrets['OPENAI_API_KEY']
openai_model = st.secrets['OPENAI_MODEL']

Keep your secrets safe

Make sure you do not share your API keys or include them in a git commit. We have added the .streamlit/secrets.toml file to the .gitignore so the contents are not accidentally uploaded to Github.

Initializing an OpenAI LLM

As the LLM will be used across the application, it makes sense to create an LLM instance that can be shared across the application.

Create a new llm.py file in the project root. Create a new variable called llm assigned to a new instance of the ChatOpenAI class.

The LLM should be initialized with an openai_api_key keyword argument. This should be set to the secret defined in the previous step.

python
llm.py
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/app-python/main/solutions/llm.py[tag=llm]

Initializing an Embedding Model

To use the Vector Search Index, you will also need to create an instance of the OpenAIEmbeddings model. This will be used by Langchain to create an embedding of the user’s input which will be used to find similar documents using Neo4j’s vector index.

python
llm.py
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/app-python/main/solutions/llm.py[tag=embedding]

Using the LLM

Once you have completed the steps, you will be able to import the llm and embeddings objects into other modules within the project.

python
from llm import llm, embeddings

That’s it!

Once you have completed the steps above, click the button to mark the lesson as completed.

Summary

In this lesson, you have created the classes required to interact with OpenAI’s LLMs.

In the next lesson, you will create the classes required to connect to Neo4j.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?