Use Luna OpenAI models
This guide will walk you through the required steps in calling and using a Luna OpenAI Model.
Luna's OpenAI models are provisioned and hosted on Azure. They are generic models, meaning they have not been fine tuned.
Models
The following models are available for use for Luna.
| Name | Model Type | Capacity | Status |
|---|---|---|---|
| luna-generic-3-large | text-embedding-3-large | 120K | In use |
| luna-generic-ada | text-embedding-ada-002 | 120K | In use |
| luna-generic-gpt35t | gpt-35-turbo-16k | 120K | In use |
| luna-generic-gpt4 | gpt-4 (1106 preview) | 60K | Available, unused |
| luna-generic-gpt4o | gpt-4o | 80K | In use |
gpt-4 is available but unused
luna-generic-gpt4 (gpt-4 (1106 preview)) is still a live Azure deployment, but no service or CLI code calls it today — luna-generic-gpt4o has taken over the chat-completion use cases. It's kept here because the deployment still exists, not because it's actively used.
INFO
If you require a model update or a different model, please reach out to the platform team and they can set one up for you.
OpenAI API with Luna Models
In order to connect and use the Azure OpenAI models you will need to configure your OpenAI client in a certain way. This should be done using the @bhag-business-platform/luna-connectors package. Please ensure you are using version 15.2.0 and higher of this package.
This package heavily uses v4+ of the OpenAI node package.
The package provides methods to aid your development. Notable methods are as follows:
| Method | Input | Description | Return |
|---|---|---|---|
chatCompletion | prompts, model | Calls the provided model with the provided prompts. | Returns the chat completion object. You can get the content using the getContentFromChatCompletion method |
createEmbedding | input, model | Creates an embedding from the given input using the given model | Returns the embedding vector |
getContentFromChatCompletion | chatCompletion | Gets the chat completion content if present | Returns the chat completion string. |
Examples
Below are a few examples of how to use these methods
chatCompletion
import { chatCompletion, getContentFromChatCompletion } from '@bhag-business-platform/luna-connectors';
const result = await chatCompletion(
[
{
role: 'system',
content: 'What is 1+1?'
}
],
'luna-generic-gpt35t'
)
const resultText = getContentFromChatCompletion(result) // 'The answer is 2'createEmbedding
import { createEmbedding } from '@bhag-business-platform/luna-connectors';
const embedding = await createEmbedding(fileString, 'luna-generic-ada')To get access to the OpenAI client you can use the getOpenAIAPIClient This will return a OpenAI client configured to an Azure OpenAI model. Ensure the model string argument is one of the models above. For example:
import { getOpenAIAPIClient } from '@bhag-business-platform/luna-connectors';
const openAI = await getOpenAIAPIClient('luna-generic-gpt4');INFO
Before calling these methods ensure the SSM credentials for the Azure OpenAI keys are set up for the environment you intend to use / run this on.
WARNING
These methods use the OpenAI Node package v 4.10.0+. This may cause issues when running tests in a jest environment.
To overcome these ensure jest and ts-jest and upgraded to version 28.0.0+
(You may need to refactor tests to use the jest.mocked method instead of just mocked)
Usage
CLI / Locally
If you are planning on using OpenAI models locally, or within a CLI type tooling. Ensure you are connected to the AND VPN when running the scripts.
Within a Lambda function
If you are planning on using OpenAI models within a Lambda function you will need to ensure your lambda is within a VPC that is white listed against the Azure OpenAI resource group.
To do this ensure your lambda has the following configuration:
# application.yml
Parameters:
...
VpcInfrastructureStackName:
Type: String
...
Resources:
...
YourLambdaFunction:
Type: AWS::Serverless::Function
Properties:
...
Policies:
...
- SSMParameterReadPolicy:
ParameterName: !Sub 'luna/${Environment}/openai-api-key'
...
VpcConfig:
SecurityGroupIds:
- !Ref YourLambdaSecurityGroup
SubnetIds:
- Fn::ImportValue:
!Sub '${VpcInfrastructureStackName}-PrivateSubnet1'
...
YourLambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
...
VpcId:
Fn::ImportValue:
!Sub '${VpcInfrastructureStackName}-VPCId'
SecurityGroupEgress:
- Description: Egress port of the http
IpProtocol: tcp
CidrIp: 0.0.0.0/0
FromPort: 80
ToPort: 80
- Description: Egress port of the https
IpProtocol: tcp
CidrIp: 0.0.0.0/0
FromPort: 443
ToPort: 443
...# samconfig.toml
...
parameter_overrides = [
...
"VpcInfrastructureStackName=\"staging-vpc-infrastructure\"" # change for production
...
]
...Example services where we use OpenAI models: chatbot, proposals, expenses, opportunities, and revenue-forecast.
