Skip to content

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.

NameModel TypeCapacityStatus
luna-generic-3-largetext-embedding-3-large120KIn use
luna-generic-adatext-embedding-ada-002120KIn use
luna-generic-gpt35tgpt-35-turbo-16k120KIn use
luna-generic-gpt4gpt-4 (1106 preview)60KAvailable, unused
luna-generic-gpt4ogpt-4o80KIn 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:

MethodInputDescriptionReturn
chatCompletionprompts, modelCalls the provided model with the provided prompts.Returns the chat completion object. You can get the content using the getContentFromChatCompletion method
createEmbeddinginput, modelCreates an embedding from the given input using the given modelReturns the embedding vector
getContentFromChatCompletionchatCompletionGets the chat completion content if presentReturns the chat completion string.

Examples ​

Below are a few examples of how to use these methods

chatCompletion ​

js
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 ​

js
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:

js
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:

yml
# 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
      ...
toml
# 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.

Made with ❤️ by the Luna team