Skip to content

Commit ed3f39d

Browse files
authored
Azure OpenAI integration (#352)
1 parent 88082ec commit ed3f39d

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ai.koog.prompt.executor.clients.openai.azure
2+
3+
import ai.koog.prompt.executor.clients.ConnectionTimeoutConfig
4+
import ai.koog.prompt.executor.clients.openai.OpenAIClientSettings
5+
6+
/**
7+
* Creates an instance of [OpenAIClientSettings] for Azure OpenAI client configuration.
8+
*
9+
* @param resourceName The name of the Azure OpenAI resource.
10+
* @param deploymentName The name of the deployment within the Azure OpenAI resource.
11+
* @param version The version of the Azure OpenAI Service to use.
12+
* @param timeoutConfig Configuration for connection timeouts, including request, connect, and socket timeouts.
13+
*/
14+
@Suppress("FunctionName")
15+
public fun AzureOpenAIClientSettings(
16+
resourceName: String,
17+
deploymentName: String,
18+
version: AzureOpenAIServiceVersion,
19+
timeoutConfig: ConnectionTimeoutConfig = ConnectionTimeoutConfig(),
20+
): OpenAIClientSettings = AzureOpenAIClientSettings(
21+
baseUrl = "https://$resourceName.openai.azure.com/openai/deployments/$deploymentName",
22+
version = version,
23+
timeoutConfig = timeoutConfig,
24+
)
25+
26+
/**
27+
* Creates an instance of [OpenAIClientSettings] for Azure OpenAI client configuration.
28+
*
29+
* This function is a convenience method that allows you to specify the base URL directly,
30+
* along with the Azure OpenAI service version and connection timeout configuration.
31+
*
32+
* @param baseUrl The base URL for the Azure OpenAI service.
33+
* @param version The version of the Azure OpenAI Service to use.
34+
* @param timeoutConfig Configuration for connection timeouts, including request, connect, and socket timeouts.
35+
*/
36+
@Suppress("FunctionName")
37+
public fun AzureOpenAIClientSettings(
38+
baseUrl: String,
39+
version: AzureOpenAIServiceVersion,
40+
timeoutConfig: ConnectionTimeoutConfig = ConnectionTimeoutConfig(),
41+
): OpenAIClientSettings = OpenAIClientSettings(
42+
baseUrl = baseUrl,
43+
timeoutConfig = timeoutConfig,
44+
chatCompletionsPath = "/chat/completions?api-version=${version.value}",
45+
embeddingsPath = "/embeddings?api-version=${version.value}",
46+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package ai.koog.prompt.executor.clients.openai.azure
2+
3+
import kotlin.jvm.JvmName
4+
import kotlin.jvm.JvmStatic
5+
6+
/**
7+
* Represents the version of the Azure OpenAI Service.
8+
*
9+
* This class encapsulates the version string and provides a way to access predefined versions.
10+
* It also allows for easy comparison and retrieval of the latest stable and preview versions.
11+
*
12+
* @property value The version string of the Azure OpenAI Service.
13+
*/
14+
public class AzureOpenAIServiceVersion private constructor(
15+
@get:JvmName("value") public val value: String,
16+
) {
17+
18+
/**
19+
* Companion object to hold predefined versions and utility methods.
20+
*/
21+
public companion object {
22+
23+
private val values: MutableMap<String, AzureOpenAIServiceVersion> =
24+
mutableMapOf<String, AzureOpenAIServiceVersion>()
25+
26+
27+
/**
28+
* Returns the latest stable version of the Azure OpenAI Service.
29+
*/
30+
@JvmStatic
31+
public fun latestStableVersion(): AzureOpenAIServiceVersion {
32+
// We can update the value every general available(GA)/stable announcement.
33+
return V2024_10_21
34+
}
35+
36+
/**
37+
* Returns the latest preview version of the Azure OpenAI Service.
38+
*/
39+
@JvmStatic
40+
public fun latestPreviewVersion(): AzureOpenAIServiceVersion {
41+
// We can update the value every preview announcement.
42+
return V2025_03_01_PREVIEW
43+
}
44+
45+
/**
46+
* Creates an instance of [AzureOpenAIServiceVersion] from a version string.
47+
*/
48+
@JvmStatic
49+
public fun fromString(version: String): AzureOpenAIServiceVersion =
50+
values.getOrPut(version) { AzureOpenAIServiceVersion(version) }
51+
52+
/**
53+
* Version 2022-12-01 of the Azure OpenAI Service.
54+
*/
55+
@JvmStatic
56+
public val V2022_12_01: AzureOpenAIServiceVersion = fromString("2022-12-01")
57+
58+
/**
59+
* Version 2023-05-15 of the Azure OpenAI Service.
60+
*/
61+
@JvmStatic
62+
public val V2023_05_15: AzureOpenAIServiceVersion = fromString("2023-05-15")
63+
64+
/**
65+
* Version 2024-02-01 of the Azure OpenAI Service.
66+
*/
67+
@JvmStatic
68+
public val V2024_02_01: AzureOpenAIServiceVersion = fromString("2024-02-01")
69+
70+
/**
71+
* Version 2024-06-01 of the Azure OpenAI Service.
72+
*/
73+
@JvmStatic
74+
public val V2024_06_01: AzureOpenAIServiceVersion = fromString("2024-06-01")
75+
76+
/**
77+
* Version 2024-10-21 of the Azure OpenAI Service.
78+
*/
79+
@JvmStatic
80+
public val V2024_10_21: AzureOpenAIServiceVersion = fromString("2024-10-21")
81+
82+
/**
83+
* Version 2023-06-01-preview of the Azure OpenAI Service.
84+
*/
85+
@JvmStatic
86+
public val V2023_06_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2023-06-01-preview")
87+
88+
/**
89+
* Version 2023-07-01-preview of the Azure OpenAI Service.
90+
*/
91+
@JvmStatic
92+
public val V2023_07_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2023-07-01-preview")
93+
94+
/**
95+
* Version 2024-02-15-preview of the Azure OpenAI Service.
96+
*/
97+
@JvmStatic
98+
public val V2024_02_15_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-02-15-preview")
99+
100+
/**
101+
* Version 2024-03-01-preview of the Azure OpenAI Service.
102+
*/
103+
@JvmStatic
104+
public val V2024_03_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-03-01-preview")
105+
106+
/**
107+
* Version 2024-04-01-preview of the Azure OpenAI Service.
108+
*/
109+
@JvmStatic
110+
public val V2024_04_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-04-01-preview")
111+
112+
/**
113+
* Version 2024-05-01-preview of the Azure OpenAI Service.
114+
*/
115+
@JvmStatic
116+
public val V2024_05_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-05-01-preview")
117+
118+
/**
119+
* Version 2024-07-01-preview of the Azure OpenAI Service.
120+
*/
121+
@JvmStatic
122+
public val V2024_07_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-07-01-preview")
123+
124+
/**
125+
* Version 2024-08-01-preview of the Azure OpenAI Service.
126+
*/
127+
@JvmStatic
128+
public val V2024_08_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-08-01-preview")
129+
130+
/**
131+
* Version 2024-09-01-preview of the Azure OpenAI Service.
132+
*/
133+
@JvmStatic
134+
public val V2024_09_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-09-01-preview")
135+
136+
/**
137+
* Version 2024-10-01-preview of the Azure OpenAI Service.
138+
*/
139+
@JvmStatic
140+
public val V2024_10_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-10-01-preview")
141+
142+
/**
143+
* Version 2024-12-01-preview of the Azure OpenAI Service.
144+
*/
145+
@JvmStatic
146+
public val V2024_12_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2024-12-01-preview")
147+
148+
/**
149+
* Version 2025-01-01-preview of the Azure OpenAI Service.
150+
*/
151+
@JvmStatic
152+
public val V2025_01_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2025-01-01-preview")
153+
154+
/**
155+
* Version 2025-02-01-preview of the Azure OpenAI Service.
156+
*/
157+
@JvmStatic
158+
public val V2025_02_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2025-02-01-preview")
159+
160+
/**
161+
* Version 2025-03-01-preview of the Azure OpenAI Service.
162+
*/
163+
@JvmStatic
164+
public val V2025_03_01_PREVIEW: AzureOpenAIServiceVersion = fromString("2025-03-01-preview")
165+
166+
}
167+
168+
override fun equals(other: Any?): Boolean =
169+
this === other || (other is AzureOpenAIServiceVersion && value == other.value)
170+
171+
override fun hashCode(): Int = value.hashCode()
172+
173+
override fun toString(): String = "AzureOpenAIServiceVersion{value=$value}"
174+
175+
}

prompt/prompt-executor/prompt-executor-llms-all/src/commonMain/kotlin/ai/koog/prompt/executor/llms/all/SimplePromptExecutors.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ai.koog.prompt.executor.llms.all
33
import ai.koog.prompt.executor.clients.anthropic.AnthropicLLMClient
44
import ai.koog.prompt.executor.clients.google.GoogleLLMClient
55
import ai.koog.prompt.executor.clients.openai.OpenAILLMClient
6+
import ai.koog.prompt.executor.clients.openai.azure.AzureOpenAIClientSettings
7+
import ai.koog.prompt.executor.clients.openai.azure.AzureOpenAIServiceVersion
68
import ai.koog.prompt.executor.clients.openrouter.OpenRouterLLMClient
79
import ai.koog.prompt.executor.llms.SingleLLMPromptExecutor
810
import ai.koog.prompt.executor.ollama.client.OllamaClient
@@ -18,6 +20,38 @@ import ai.koog.prompt.executor.ollama.client.OllamaClient
1820
*/
1921
public fun simpleOpenAIExecutor(apiToken: String): SingleLLMPromptExecutor = SingleLLMPromptExecutor(OpenAILLMClient(apiToken))
2022

23+
/**
24+
* Creates an instance of `SingleLLMPromptExecutor` with an `OpenAILLMClient` configured for Azure OpenAI.
25+
*
26+
* @param resourceName The name of the Azure OpenAI resource.
27+
* @param deploymentName The name of the deployment within the Azure OpenAI resource.
28+
* @param version The version of the Azure OpenAI Service to use.
29+
* @param apiToken The API token used for authentication with the Azure OpenAI service.
30+
* @return A new instance of `SingleLLMPromptExecutor` configured with the `OpenAILLMClient` for Azure OpenAI.
31+
*/
32+
public fun simpleAzureOpenAIExecutor(
33+
resourceName: String,
34+
deploymentName: String,
35+
version: AzureOpenAIServiceVersion,
36+
apiToken: String,
37+
): SingleLLMPromptExecutor =
38+
SingleLLMPromptExecutor(OpenAILLMClient(apiToken, AzureOpenAIClientSettings(resourceName, deploymentName, version)))
39+
40+
/**
41+
* Creates an instance of `SingleLLMPromptExecutor` with an `OpenAILLMClient` configured for Azure OpenAI.
42+
*
43+
* @param baseUrl The base URL for the Azure OpenAI service.
44+
* @param version The version of the Azure OpenAI Service to use.
45+
* @param apiToken The API token used for authentication with the Azure OpenAI service.
46+
* @return A new instance of `SingleLLMPromptExecutor` configured with the `OpenAILLMClient` for Azure OpenAI.
47+
*/
48+
public fun simpleAzureOpenAIExecutor(
49+
baseUrl: String,
50+
version: AzureOpenAIServiceVersion,
51+
apiToken: String,
52+
): SingleLLMPromptExecutor =
53+
SingleLLMPromptExecutor(OpenAILLMClient(apiToken, AzureOpenAIClientSettings(baseUrl, version)))
54+
2155
/**
2256
* Creates an instance of `SingleLLMPromptExecutor` with an `AnthropicLLMClient`.
2357
*

0 commit comments

Comments
 (0)