Skip to content

Commit

Permalink
add OpenAI organization ID to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Mar 21, 2024
1 parent 32fef84 commit c0c2801
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class AIServiceImpl implements AIService {
private RateLimiter rateLimiter;
private Gson gson = new Gson();
private CloseableHttpClient httpClient;
private String organizationId;

@Override
public boolean isAvailable() {
Expand All @@ -81,7 +82,7 @@ public String prompt(@Nullable String systemmsg, @Nonnull String usermsg, @Nulla
userMessage.put("content", usermsg);

Map<String, Object> request = new HashMap<>();
request.put("model", StringUtils.defaultString(config.defaultModel(), DEFAULT_MODEL));
request.put("model", StringUtils.defaultIfBlank(config.defaultModel(), DEFAULT_MODEL));
if (systemmsg != null) {
Map<String, Object> systemMessage = new LinkedHashMap<>();
systemMessage.put("role", "system");
Expand All @@ -108,6 +109,9 @@ public String prompt(@Nullable String systemmsg, @Nonnull String usermsg, @Nulla
entityBuilder.setText(requestJson);
postRequest.setEntity(entityBuilder.build());
postRequest.setHeader("Authorization", "Bearer " + openAiApiKey);
if (organizationId != null) {
postRequest.setHeader("OpenAI-Organization", organizationId);
}
String id = "#" + System.nanoTime();
LOG.debug("Request {} to OpenAI: {}", id, requestJson);
try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
Expand Down Expand Up @@ -180,6 +184,7 @@ protected void activate(Configuration configuration) {
}
}
openAiApiKey = StringUtils.trimToNull(openAiApiKey);
organizationId = StringUtils.trimToNull(config.organizationId());
rateLimiter = null;
if (isAvailable()) {
int perMinuteLimit = config.requestsPerMinuteLimit() > 0 ? config.requestsPerMinuteLimit() : 20;
Expand All @@ -205,6 +210,9 @@ protected void activate(Configuration configuration) {
@AttributeDefinition(name = "OpenAI API Key File containing the API key, as an alternative to Open AKI Key configuration and the variants described there.")
String openAiApiKeyFile();

@AttributeDefinition(name = "Organization ID", description = "The organization ID from OpenAI, if you have one.")
String organizationId();

@AttributeDefinition(name = "Default model to use for the chat completion. If not configured we take a default, in this version " + DEFAULT_MODEL + ". Please consider the varying prices https://openai.com/pricing . For programming related questions a GPT-4 seems necessary, though. Do not configure if not necessary, to follow further changes.")
String defaultModel();

Expand Down

0 comments on commit c0c2801

Please sign in to comment.