langfun 0.1.2.dev202503040804__py3-none-any.whl → 0.1.2.dev202503060804__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langfun/core/concurrent_test.py +1 -1
- langfun/core/eval/v2/checkpointing.py +1 -1
- langfun/core/llms/__init__.py +1 -0
- langfun/core/llms/azure_openai.py +90 -0
- langfun/core/llms/azure_openai_test.py +47 -0
- {langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/METADATA +1 -1
- {langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/RECORD +10 -8
- {langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/LICENSE +0 -0
- {langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/WHEEL +0 -0
- {langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/top_level.txt +0 -0
langfun/core/concurrent_test.py
CHANGED
@@ -560,6 +560,7 @@ class ConcurrentMapTest(unittest.TestCase):
|
|
560
560
|
fun, [1, 2, 3], timeout=1.5, max_workers=1, show_progress=True
|
561
561
|
)
|
562
562
|
], key=lambda x: x[0])
|
563
|
+
string_io.flush()
|
563
564
|
self.assertEqual( # pylint: disable=g-generic-assert
|
564
565
|
output,
|
565
566
|
[
|
@@ -570,7 +571,6 @@ class ConcurrentMapTest(unittest.TestCase):
|
|
570
571
|
)
|
571
572
|
output = string_io.getvalue()
|
572
573
|
self.assertIn('100%', output)
|
573
|
-
self.assertIn('TimeIt=foo (', output)
|
574
574
|
|
575
575
|
def test_concurrent_map_with_showing_progress_and_status_fn(self):
|
576
576
|
def fun(x):
|
langfun/core/llms/__init__.py
CHANGED
@@ -36,6 +36,7 @@ from langfun.core.llms.anthropic import Anthropic
|
|
36
36
|
# Base models by serving platforms.
|
37
37
|
from langfun.core.llms.vertexai import VertexAI
|
38
38
|
from langfun.core.llms.groq import Groq
|
39
|
+
from langfun.core.llms.azure_openai import AzureOpenAI
|
39
40
|
|
40
41
|
# Gemini models.
|
41
42
|
from langfun.core.llms.google_genai import GenAI
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# Copyright 2025 The Langfun Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""OpenAI models hosted on Azure."""
|
15
|
+
import os
|
16
|
+
from typing import Annotated, Any
|
17
|
+
|
18
|
+
import langfun.core as lf
|
19
|
+
from langfun.core.llms import openai
|
20
|
+
import pyglove as pg
|
21
|
+
|
22
|
+
|
23
|
+
@lf.use_init_args(['model', 'deployment_name'])
|
24
|
+
@pg.members([('api_endpoint', pg.typing.Str().freeze(''))])
|
25
|
+
class AzureOpenAI(openai.OpenAI):
|
26
|
+
"""Azure OpenAI model service.
|
27
|
+
|
28
|
+
This service interacts with the Azure OpenAI API to generate chat completions.
|
29
|
+
It uses the deployment_name and API version to construct the endpoint, and
|
30
|
+
authenticates using an API key provided via parameter or the
|
31
|
+
AZURE_OPENAI_API_KEY environment variable.
|
32
|
+
|
33
|
+
Example:
|
34
|
+
lm = AzureOpenAI(
|
35
|
+
model='gpt-4o',
|
36
|
+
deployment_name='gpt-4o',
|
37
|
+
api_version='2024-08-01-preview',
|
38
|
+
azure_endpoint='https://trackname.openai.azure.com/',
|
39
|
+
api_key='token'
|
40
|
+
)
|
41
|
+
response = lf.query(prompt="what the capital of France", lm=lm)
|
42
|
+
print(response)
|
43
|
+
"""
|
44
|
+
|
45
|
+
deployment_name: Annotated[
|
46
|
+
str,
|
47
|
+
'The name of the Azure OpenAI deployment.'
|
48
|
+
] = 'gpt-4'
|
49
|
+
|
50
|
+
api_version: Annotated[
|
51
|
+
str,
|
52
|
+
'The API version for Azure OpenAI.'
|
53
|
+
] = '2023-05-15'
|
54
|
+
|
55
|
+
azure_endpoint: Annotated[
|
56
|
+
str,
|
57
|
+
(
|
58
|
+
'The base URL for Azure OpenAI '
|
59
|
+
'(e.g. "https://<your-resource>.openai.azure.com/")'
|
60
|
+
)
|
61
|
+
] = 'https://api.openai.azure.com/'
|
62
|
+
|
63
|
+
def _initialize(self):
|
64
|
+
# Authentication
|
65
|
+
self._api_key = self.api_key or os.environ.get('AZURE_OPENAI_API_KEY')
|
66
|
+
if not self._api_key:
|
67
|
+
raise ValueError(
|
68
|
+
'Azure OpenAI requires an API key. Please provide '
|
69
|
+
'via `api_key` argument or AZURE_OPENAI_API_KEY '
|
70
|
+
'environment variable.'
|
71
|
+
)
|
72
|
+
|
73
|
+
# Endpoint construction
|
74
|
+
self._api_endpoint = (
|
75
|
+
f"{self.azure_endpoint.rstrip('/')}/openai/deployments/"
|
76
|
+
f"{self.deployment_name}/chat/completions"
|
77
|
+
f"?api-version={self.api_version}"
|
78
|
+
)
|
79
|
+
|
80
|
+
@property
|
81
|
+
def api_endpoint(self) -> str:
|
82
|
+
return self._api_endpoint
|
83
|
+
|
84
|
+
@property
|
85
|
+
def headers(self) -> dict[str, Any]:
|
86
|
+
headers = super().headers
|
87
|
+
headers.update({
|
88
|
+
'api-key': self._api_key,
|
89
|
+
})
|
90
|
+
return headers
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# Copyright 2025 The Langfun Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
import os
|
15
|
+
import unittest
|
16
|
+
from langfun.core.llms import azure_openai
|
17
|
+
|
18
|
+
|
19
|
+
class AzureOpenAITest(unittest.TestCase):
|
20
|
+
def test_api_key_missing(self):
|
21
|
+
# Ensure that ValueError is raised when API key is not provided
|
22
|
+
os.environ.pop('AZURE_OPENAI_API_KEY', None)
|
23
|
+
with self.assertRaisesRegex(ValueError, 'Azure OpenAI requires an API key'):
|
24
|
+
azure = azure_openai.AzureOpenAI(model='gpt-4')
|
25
|
+
azure._initialize()
|
26
|
+
|
27
|
+
def test_api_endpoint(self):
|
28
|
+
# Test that api_endpoint is properly constructed
|
29
|
+
azure = azure_openai.AzureOpenAI(model='gpt-4', api_key='test_key')
|
30
|
+
azure._initialize()
|
31
|
+
expected = (
|
32
|
+
'https://api.openai.azure.com/openai/deployments/gpt-4/chat/'
|
33
|
+
'completions?api-version=2023-05-15'
|
34
|
+
)
|
35
|
+
self.assertEqual(azure.api_endpoint, expected)
|
36
|
+
|
37
|
+
def test_headers(self):
|
38
|
+
# Test that headers contain the api-key with correct value
|
39
|
+
azure = azure_openai.AzureOpenAI(model='gpt-4', api_key='test_key')
|
40
|
+
azure._initialize()
|
41
|
+
headers = azure.headers
|
42
|
+
self.assertIn('api-key', headers)
|
43
|
+
self.assertEqual(headers.get('api-key'), 'test_key')
|
44
|
+
|
45
|
+
|
46
|
+
if __name__ == '__main__':
|
47
|
+
unittest.main()
|
@@ -3,7 +3,7 @@ langfun/core/__init__.py,sha256=S6YS15fLy5M3L3qaSa3XhDgku-klUm1LJxFPB-Wl-tY,4596
|
|
3
3
|
langfun/core/component.py,sha256=g1kQM0bryYYYWVDrSMnHfc74wIBbpfe5_B3s-UIP5GE,3028
|
4
4
|
langfun/core/component_test.py,sha256=0CxTgjAud3aj8wBauFhG2FHDqrxCTl4OI4gzQTad-40,9254
|
5
5
|
langfun/core/concurrent.py,sha256=zY-pXqlGqss_GI20tM1gXvyW8QepVPUuFNmutcIdhbI,32760
|
6
|
-
langfun/core/concurrent_test.py,sha256=
|
6
|
+
langfun/core/concurrent_test.py,sha256=roMFze0EKuyPbmG6DZzz8K8VGsZwWzc0F1uJZTFROC4,17572
|
7
7
|
langfun/core/console.py,sha256=V_mOiFi9oGh8gLsUeR56pdFDkuvYOpvQt7DY1KUTWTA,2535
|
8
8
|
langfun/core/console_test.py,sha256=pBOcuNMJdVELywvroptfcRtJMsegMm3wSlHAL2TdxVk,1679
|
9
9
|
langfun/core/langfunc.py,sha256=G50YgoVZ0y1GFw2ev41MlOqr6qa8YakbvNC0h_E0PiA,11140
|
@@ -52,7 +52,7 @@ langfun/core/eval/patching_test.py,sha256=8kCd54Egjju22FMgtJuxEsrXkW8ifs-UUBHtrC
|
|
52
52
|
langfun/core/eval/scoring.py,sha256=_DvnlgI1SdRVaOojao_AkV3pnenfCPOqyhvlg-Sw-5M,6322
|
53
53
|
langfun/core/eval/scoring_test.py,sha256=adQEeuDux11dt9lkJIzLYNmqYSi9h-Mi2Cr0lUUTx9I,4546
|
54
54
|
langfun/core/eval/v2/__init__.py,sha256=qoa6zKdFXOFyCX6vay6OdgPf1eUhYGoHYAxe35qECGk,1628
|
55
|
-
langfun/core/eval/v2/checkpointing.py,sha256=
|
55
|
+
langfun/core/eval/v2/checkpointing.py,sha256=esGX-fB5qr_DEFoYOFapl-Ve7VEtX0_xTK45TYKC4xo,11771
|
56
56
|
langfun/core/eval/v2/checkpointing_test.py,sha256=R-R8SFzworuYnMmGGcvE9f4oiYkb8HMoZ0m4euF3pus,8466
|
57
57
|
langfun/core/eval/v2/eval_test_helper.py,sha256=A9w0FbRKieB3yym8x34kY24FfptHrp7m6_z3i8HXSFI,3922
|
58
58
|
langfun/core/eval/v2/evaluation.py,sha256=PgX1DoG3z-En7KVZe2hgSI7sMrLodvtT_UcUGMkQ8FQ,22562
|
@@ -73,9 +73,11 @@ langfun/core/eval/v2/reporting.py,sha256=7rL9LLmGYnQ5HIjqRqsOMkUlBl4BmFPEL6Vlofq
|
|
73
73
|
langfun/core/eval/v2/reporting_test.py,sha256=UmYSAQvD3AIXsSyWQ-WD2uLtEISYpmBeoKY5u5Qwc8E,5696
|
74
74
|
langfun/core/eval/v2/runners.py,sha256=DKEmSlGXjOXKWFdBhTpLy7tMsBHZHd1Brl3hWIngsSQ,15931
|
75
75
|
langfun/core/eval/v2/runners_test.py,sha256=A37fKK2MvAVTiShsg_laluJzJ9AuAQn52k7HPbfD0Ks,11666
|
76
|
-
langfun/core/llms/__init__.py,sha256=
|
76
|
+
langfun/core/llms/__init__.py,sha256=F2nqEv9Cc-1QALrlPnIEp-TZiSVTi6U8WWb1YwDt1w0,8038
|
77
77
|
langfun/core/llms/anthropic.py,sha256=7kY1-QFpzh2Oye8H1Mtko7QJWrnrhnhe1Wj16nOZXvk,23473
|
78
78
|
langfun/core/llms/anthropic_test.py,sha256=SSK7OTx3gMYE1NMAi_PqQqeNsCkZAcVJvl_OCEOhyzk,7145
|
79
|
+
langfun/core/llms/azure_openai.py,sha256=-KkSLaR54MlsIqz_XIwv0TnsBnvNTAxnjA2Q2O2u5KM,2733
|
80
|
+
langfun/core/llms/azure_openai_test.py,sha256=lkMZkQdJBV97fTM4C4z8qNfvr6spgiN5G4hvVUIVr0M,1735
|
79
81
|
langfun/core/llms/compositional.py,sha256=csW_FLlgL-tpeyCOTVvfUQkMa_zCN5Y2I-YbSNuK27U,2872
|
80
82
|
langfun/core/llms/compositional_test.py,sha256=4eTnOer-DncRKGaIJW2ZQQMLnt5r2R0UIx_DYOvGAQo,2027
|
81
83
|
langfun/core/llms/deepseek.py,sha256=jvTxdXPr-vH6HNakn_Ootx1heDg8Fen2FUkUW36bpCs,5247
|
@@ -148,8 +150,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
|
|
148
150
|
langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
|
149
151
|
langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
|
150
152
|
langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
|
151
|
-
langfun-0.1.2.
|
152
|
-
langfun-0.1.2.
|
153
|
-
langfun-0.1.2.
|
154
|
-
langfun-0.1.2.
|
155
|
-
langfun-0.1.2.
|
153
|
+
langfun-0.1.2.dev202503060804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
154
|
+
langfun-0.1.2.dev202503060804.dist-info/METADATA,sha256=EjrGcGHR07d5MlmqA2tOO1pGzISqvGXUHpaTCWHjNzM,8172
|
155
|
+
langfun-0.1.2.dev202503060804.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
|
156
|
+
langfun-0.1.2.dev202503060804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
|
157
|
+
langfun-0.1.2.dev202503060804.dist-info/RECORD,,
|
File without changes
|
File without changes
|
{langfun-0.1.2.dev202503040804.dist-info → langfun-0.1.2.dev202503060804.dist-info}/top_level.txt
RENAMED
File without changes
|