langfun 0.0.2.dev20240717__py3-none-any.whl → 0.1.0__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/__init__.py +1 -1
- langfun/core/language_model.py +7 -4
- langfun/core/modalities/image.py +6 -0
- langfun/core/modalities/image_test.py +6 -0
- langfun/core/structured/prompting.py +5 -0
- langfun/core/structured/prompting_test.py +11 -0
- langfun-0.1.0.dist-info/METADATA +168 -0
- {langfun-0.0.2.dev20240717.dist-info → langfun-0.1.0.dist-info}/RECORD +11 -11
- {langfun-0.0.2.dev20240717.dist-info → langfun-0.1.0.dist-info}/WHEEL +1 -1
- langfun-0.0.2.dev20240717.dist-info/METADATA +0 -103
- {langfun-0.0.2.dev20240717.dist-info → langfun-0.1.0.dist-info}/LICENSE +0 -0
- {langfun-0.0.2.dev20240717.dist-info → langfun-0.1.0.dist-info}/top_level.txt +0 -0
langfun/__init__.py
CHANGED
langfun/core/language_model.py
CHANGED
@@ -17,6 +17,7 @@ import abc
|
|
17
17
|
import contextlib
|
18
18
|
import dataclasses
|
19
19
|
import enum
|
20
|
+
import threading
|
20
21
|
import time
|
21
22
|
from typing import Annotated, Any, Callable, Iterator, Sequence, Tuple, Type, Union
|
22
23
|
from langfun.core import component
|
@@ -728,6 +729,7 @@ class _UsageTracker:
|
|
728
729
|
|
729
730
|
def __init__(self, model_ids: set[str] | None):
|
730
731
|
self.model_ids = model_ids
|
732
|
+
self._lock = threading.Lock()
|
731
733
|
self.usages = {
|
732
734
|
m: LMSamplingUsage(0, 0, 0, 0) for m in model_ids
|
733
735
|
} if model_ids else {}
|
@@ -735,10 +737,11 @@ class _UsageTracker:
|
|
735
737
|
def track(self, model_id: str, usage: LMSamplingUsage):
|
736
738
|
if self.model_ids is not None and model_id not in self.model_ids:
|
737
739
|
return
|
738
|
-
|
739
|
-
self.usages
|
740
|
-
|
741
|
-
|
740
|
+
with self._lock:
|
741
|
+
if not isinstance(usage, UsageNotAvailable) and model_id in self.usages:
|
742
|
+
self.usages[model_id] += usage
|
743
|
+
else:
|
744
|
+
self.usages[model_id] = usage
|
742
745
|
|
743
746
|
|
744
747
|
@contextlib.contextmanager
|
langfun/core/modalities/image.py
CHANGED
@@ -14,7 +14,9 @@
|
|
14
14
|
"""Image modality."""
|
15
15
|
|
16
16
|
import functools
|
17
|
+
import io
|
17
18
|
from langfun.core.modalities import mime
|
19
|
+
from PIL import Image as pil_image
|
18
20
|
|
19
21
|
|
20
22
|
class Image(mime.Mime):
|
@@ -28,3 +30,7 @@ class Image(mime.Mime):
|
|
28
30
|
|
29
31
|
def _html(self, uri: str) -> str:
|
30
32
|
return f'<img src="{uri}">'
|
33
|
+
|
34
|
+
def size(self) -> tuple[int, int]:
|
35
|
+
img = pil_image.open(io.BytesIO(self.to_bytes()))
|
36
|
+
return img.size
|
@@ -77,6 +77,12 @@ class ImageTest(unittest.TestCase):
|
|
77
77
|
self.assertEqual(image._repr_html_(), '<img src="http://mock/web/a.png">')
|
78
78
|
self.assertEqual(image.to_bytes(), image_content)
|
79
79
|
|
80
|
+
def test_image_size(self):
|
81
|
+
image = image_lib.Image.from_uri('http://mock/web/a.png')
|
82
|
+
with mock.patch('requests.get') as mock_requests_get:
|
83
|
+
mock_requests_get.side_effect = mock_request
|
84
|
+
self.assertEqual(image.size(), (24, 24))
|
85
|
+
|
80
86
|
|
81
87
|
if __name__ == '__main__':
|
82
88
|
unittest.main()
|
@@ -193,6 +193,11 @@ def query(
|
|
193
193
|
if isinstance(prompt, pg.Symbolic) and prompt.sym_partial and schema is None:
|
194
194
|
schema = prompt.__class__
|
195
195
|
|
196
|
+
# Create a copy of the prompt if it has a parent object, so all child modality
|
197
|
+
# objects could be referred by path relative to the prompt.
|
198
|
+
if isinstance(prompt, lf.Template) and prompt.sym_parent:
|
199
|
+
prompt = prompt.clone()
|
200
|
+
|
196
201
|
if schema in (None, str):
|
197
202
|
# Query with natural language output.
|
198
203
|
output = lf.LangFunc.from_value(prompt, **kwargs)(
|
@@ -361,6 +361,17 @@ class QueryTest(unittest.TestCase):
|
|
361
361
|
"""),
|
362
362
|
)
|
363
363
|
|
364
|
+
def test_query_prompt_with_unrooted_template(self):
|
365
|
+
output = prompting.query_prompt(
|
366
|
+
pg.Dict(
|
367
|
+
input=lf.Template(
|
368
|
+
'what is {{image}}',
|
369
|
+
image=modalities.Image.from_bytes(b'mock_image')
|
370
|
+
)
|
371
|
+
).input,
|
372
|
+
)
|
373
|
+
self.assertIsNotNone(output.get_modality('image'))
|
374
|
+
|
364
375
|
def test_query_output(self):
|
365
376
|
self.assertEqual(
|
366
377
|
prompting.query_output(
|
@@ -0,0 +1,168 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: langfun
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Langfun: Language as Functions.
|
5
|
+
Home-page: https://github.com/google/langfun
|
6
|
+
Author: Langfun Authors
|
7
|
+
Author-email: langfun-authors@google.com
|
8
|
+
License: Apache License 2.0
|
9
|
+
Keywords: llm generative-ai machine-learning
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
11
|
+
Classifier: Intended Audience :: Developers
|
12
|
+
Classifier: Intended Audience :: Education
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Human Machine Interfaces
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
22
|
+
Description-Content-Type: text/markdown
|
23
|
+
License-File: LICENSE
|
24
|
+
Requires-Dist: google-cloud-aiplatform >=1.5.0
|
25
|
+
Requires-Dist: google-generativeai >=0.3.2
|
26
|
+
Requires-Dist: jinja2 >=3.1.2
|
27
|
+
Requires-Dist: openai ==0.27.2
|
28
|
+
Requires-Dist: openpyxl >=3.1.0
|
29
|
+
Requires-Dist: pandas >=2.1.4
|
30
|
+
Requires-Dist: pyglove >=0.4.5.dev20240423
|
31
|
+
Requires-Dist: python-docx >=0.8.11
|
32
|
+
Requires-Dist: python-magic >=0.4.27
|
33
|
+
Requires-Dist: requests >=2.31.0
|
34
|
+
Requires-Dist: termcolor ==1.1.0
|
35
|
+
Requires-Dist: tqdm >=4.64.1
|
36
|
+
Requires-Dist: pillow >=10.1.0
|
37
|
+
|
38
|
+
<div align="center">
|
39
|
+
<img src="https://raw.githubusercontent.com/google/langfun/main/docs/_static/logo.svg" width="520px" alt="logo"></img>
|
40
|
+
</div>
|
41
|
+
|
42
|
+
# Langfun
|
43
|
+
|
44
|
+
[](https://badge.fury.io/py/langfun)
|
45
|
+
[](https://codecov.io/gh/google/langfun)
|
46
|
+

|
47
|
+
|
48
|
+
[**Installation**](#install) | [**Getting started**](#hello-langfun) | [**Tutorial**](https://colab.research.google.com/github/google/langfun/blob/main/docs/notebooks/langfun101.ipynb)
|
49
|
+
|
50
|
+
## Introduction
|
51
|
+
|
52
|
+
Langfun is a [PyGlove](https://github.com/google/pyglove) powered library that
|
53
|
+
aims to *make language models (LM) fun to work with*. Its central principle is
|
54
|
+
to enable seamless integration between natural language and programming by
|
55
|
+
treating language as functions. Through the introduction of *Object-Oriented Prompting*,
|
56
|
+
Langfun empowers users to prompt LLMs using objects and types, offering enhanced
|
57
|
+
control and simplifying agent development.
|
58
|
+
|
59
|
+
To unlock the magic of Langfun, you can start with
|
60
|
+
[Langfun 101](https://colab.research.google.com/github/google/langfun/blob/main/docs/notebooks/langfun101.ipynb). Notably, Langfun is compatible with popular LLMs such as Gemini, GPT,
|
61
|
+
Claude, all without the need for additional fine-tuning.
|
62
|
+
|
63
|
+
## Why Langfun?
|
64
|
+
|
65
|
+
Langfun is *powerful and scalable*:
|
66
|
+
|
67
|
+
* Seamless integration between natural language and computer programs.
|
68
|
+
* Modular prompts, which allows a natural blend of texts and modalities;
|
69
|
+
* Efficient for both request-based workflows and batch jobs;
|
70
|
+
* A powerful eval framework that thrives dimension explosions.
|
71
|
+
|
72
|
+
Langfun is *simple and elegant*:
|
73
|
+
|
74
|
+
* An intuitive programming model, graspable in 5 minutes;
|
75
|
+
* Plug-and-play into any Python codebase, making an immediate difference;
|
76
|
+
* Comprehensive LLMs under a unified API: Gemini, GPT, Claude, Llama3, and more.
|
77
|
+
* Designed for agile developement: offering intellisense, easy debugging, with minimal overhead;
|
78
|
+
|
79
|
+
## Hello, Langfun
|
80
|
+
|
81
|
+
```python
|
82
|
+
import langfun as lf
|
83
|
+
import pyglove as pg
|
84
|
+
|
85
|
+
from typing import Literal
|
86
|
+
from IPython import display
|
87
|
+
|
88
|
+
class Item(pg.Object):
|
89
|
+
name: str
|
90
|
+
size: Literal['large', 'medium', 'small']
|
91
|
+
|
92
|
+
class ImageDescription(pg.Object):
|
93
|
+
items: list[Item]
|
94
|
+
|
95
|
+
image = lf.Image.from_uri('https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Solar_system.jpg/1646px-Solar_system.jpg')
|
96
|
+
display.display(image)
|
97
|
+
|
98
|
+
desc = lf.query(
|
99
|
+
'Describe objects in {{my_image}} from top to bottom.',
|
100
|
+
ImageDescription,
|
101
|
+
lm=lf.llms.Gpt4o(api_key='<your-openai-api-key>'),
|
102
|
+
my_image=image,
|
103
|
+
)
|
104
|
+
print(desc)
|
105
|
+
```
|
106
|
+
*Output:*
|
107
|
+
|
108
|
+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Solar_system.jpg/1646px-Solar_system.jpg" width="520px" alt="my_image"></img>
|
109
|
+
|
110
|
+
```
|
111
|
+
ImageDescription(
|
112
|
+
items = [
|
113
|
+
0 : Item(
|
114
|
+
name = 'Mercury',
|
115
|
+
color = 'Gray'
|
116
|
+
),
|
117
|
+
1 : Item(
|
118
|
+
name = 'Venus',
|
119
|
+
color = 'Yellow'
|
120
|
+
),
|
121
|
+
2 : Item(
|
122
|
+
name = 'Earth',
|
123
|
+
color = 'Blue and white'
|
124
|
+
),
|
125
|
+
3 : Item(
|
126
|
+
name = 'Moon',
|
127
|
+
color = 'Gray'
|
128
|
+
),
|
129
|
+
4 : Item(
|
130
|
+
name = 'Mars',
|
131
|
+
color = 'Red'
|
132
|
+
),
|
133
|
+
5 : Item(
|
134
|
+
name = 'Jupiter',
|
135
|
+
color = 'Brown and white'
|
136
|
+
),
|
137
|
+
6 : Item(
|
138
|
+
name = 'Saturn',
|
139
|
+
color = 'Yellowish-brown with rings'
|
140
|
+
),
|
141
|
+
7 : Item(
|
142
|
+
name = 'Uranus',
|
143
|
+
color = 'Light blue'
|
144
|
+
),
|
145
|
+
8 : Item(
|
146
|
+
name = 'Neptune',
|
147
|
+
color = 'Dark blue'
|
148
|
+
)
|
149
|
+
]
|
150
|
+
)
|
151
|
+
```
|
152
|
+
See [Langfun 101](https://colab.research.google.com/github/google/langfun/blob/main/docs/notebooks/langfun101.ipynb) for more examples.
|
153
|
+
|
154
|
+
## Install
|
155
|
+
|
156
|
+
```
|
157
|
+
pip install langfun
|
158
|
+
```
|
159
|
+
|
160
|
+
Or install nightly build with:
|
161
|
+
|
162
|
+
```
|
163
|
+
pip install langfun --pre
|
164
|
+
```
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
*Disclaimer: this is not an officially supported Google product.*
|
@@ -1,4 +1,4 @@
|
|
1
|
-
langfun/__init__.py,sha256=
|
1
|
+
langfun/__init__.py,sha256=mPN6TszWxnQKhlnMGvEvu56etujXb1Zcy4DDMUy1IH8,2274
|
2
2
|
langfun/core/__init__.py,sha256=Mdp1a2YnXdSmfTfbUwuAnEWYbjA3rXXGtbxl5fljZyg,4812
|
3
3
|
langfun/core/component.py,sha256=Icyoj9ICoJoK2r2PHbrFXbxnseOr9QZZOvKWklLWNo8,10276
|
4
4
|
langfun/core/component_test.py,sha256=q15Xn51cVTu2RKxZ9U5VQgT3bm6RQ4638bKhWBtvW5o,8220
|
@@ -8,7 +8,7 @@ langfun/core/console.py,sha256=bk5rNPNm9rMGW5YT2HixxU04p2umnoabn5SDz6Dqe88,2317
|
|
8
8
|
langfun/core/console_test.py,sha256=5SYJdxpJGLgdSSQqqMPoA1X6jpsLD8rgcyk-EgI65oE,1077
|
9
9
|
langfun/core/langfunc.py,sha256=RvIcRjIq0jWYRu1xim-FYe4HSrt97r3GMBO_PuagUmw,11060
|
10
10
|
langfun/core/langfunc_test.py,sha256=lyt-UzkD8972cxZwzCkps0_RMLeSsOBrcUFIW-fB6us,8653
|
11
|
-
langfun/core/language_model.py,sha256=
|
11
|
+
langfun/core/language_model.py,sha256=ihcLy7WWrUByZ4Yfikb2OBppM6QGwMyjTYecBzelNCs,24028
|
12
12
|
langfun/core/language_model_test.py,sha256=TlNmVUfBfDQZzIiiBqCBTrxgcoyj2qNp3kONvmr2pX4,21273
|
13
13
|
langfun/core/logging.py,sha256=FyZRxUy2TTF6tWLhQCRpCvfH55WGUdNgQjUTK_SQLnY,5320
|
14
14
|
langfun/core/logging_test.py,sha256=qvm3RObYP3knO2PnXR9evBRl4gH621GnjnwywbGbRfg,1833
|
@@ -79,8 +79,8 @@ langfun/core/memories/conversation_history_test.py,sha256=AaW8aNoFjxNusanwJDV0r3
|
|
79
79
|
langfun/core/modalities/__init__.py,sha256=F8P72IwFiTpEseTR2tYEJyQMlDW7fd9csvGJquLKJNg,1269
|
80
80
|
langfun/core/modalities/audio.py,sha256=Qxo7bYjLKQ1gVJVomr9RqR2SvxY826QgXhTzzk437Sk,952
|
81
81
|
langfun/core/modalities/audio_test.py,sha256=gWCB9h3FyrdGqro3ajBXqkw0lU0W1sBjOOq6wZbl7Fg,2027
|
82
|
-
langfun/core/modalities/image.py,sha256=
|
83
|
-
langfun/core/modalities/image_test.py,sha256=
|
82
|
+
langfun/core/modalities/image.py,sha256=sMMmjNYUTdPViDOqc93WtSFrzE1-GRrEv-6I3eb8iYY,1083
|
83
|
+
langfun/core/modalities/image_test.py,sha256=Rcq4b77E9-Vp6rhqJ5ZIh0yJkEtcJbZZwjTjlu9dx8E,3282
|
84
84
|
langfun/core/modalities/mime.py,sha256=7oYvwYZHb3uhjiL2rF6kvQWsWufY0UXl72wfDXC59ys,5918
|
85
85
|
langfun/core/modalities/mime_test.py,sha256=kmRiPP-3Py02NnKPu0oPexSIJ-MXaaE2UYY82ga0TV8,2913
|
86
86
|
langfun/core/modalities/ms_office.py,sha256=jOidMSdWCaV9RILpGz8VJkpTSpHJNoirD53jzQvcytM,3388
|
@@ -100,8 +100,8 @@ langfun/core/structured/mapping.py,sha256=QKbSnvOgut-sx2mZPjHJcdlDLxR8b3ZC16ZLWo
|
|
100
100
|
langfun/core/structured/mapping_test.py,sha256=PiXklMeIa8L6KtMi3ju7J9Y39gZy0hIGz-Oeq4A_7XE,3835
|
101
101
|
langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
|
102
102
|
langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
|
103
|
-
langfun/core/structured/prompting.py,sha256=
|
104
|
-
langfun/core/structured/prompting_test.py,sha256=
|
103
|
+
langfun/core/structured/prompting.py,sha256=_U6Z65AwXvVvfaQFCY9GawB_QV9S3u7P7BOU2URABmw,8873
|
104
|
+
langfun/core/structured/prompting_test.py,sha256=Yc5GevbTyxtW6FAORXiqzakcuoJF-IPjoZ8kvLJW5is,23057
|
105
105
|
langfun/core/structured/schema.py,sha256=oiT4P4Q9pG-QOnFzxETN2EQZqNln8nG4zAJHxcmeX9U,27729
|
106
106
|
langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
|
107
107
|
langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
|
@@ -117,8 +117,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
|
|
117
117
|
langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
|
118
118
|
langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
|
119
119
|
langfun/core/templates/selfplay_test.py,sha256=rBW2Qr8yi-aWYwoTwRR-n1peKyMX9QXPZXURjLgoiRs,2264
|
120
|
-
langfun-0.0.
|
121
|
-
langfun-0.0.
|
122
|
-
langfun-0.0.
|
123
|
-
langfun-0.0.
|
124
|
-
langfun-0.0.
|
120
|
+
langfun-0.1.0.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
121
|
+
langfun-0.1.0.dist-info/METADATA,sha256=MUO2hj8fPm1zMlG9yIN8x0Ct1zisC39u769k5TlPJG0,5293
|
122
|
+
langfun-0.1.0.dist-info/WHEEL,sha256=-oYQCr74JF3a37z2nRlQays_SX2MqOANoqVjBBAP2yE,91
|
123
|
+
langfun-0.1.0.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
|
124
|
+
langfun-0.1.0.dist-info/RECORD,,
|
@@ -1,103 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: langfun
|
3
|
-
Version: 0.0.2.dev20240717
|
4
|
-
Summary: Langfun: Language as Functions.
|
5
|
-
Home-page: https://github.com/google/langfun
|
6
|
-
Author: Langfun Authors
|
7
|
-
Author-email: langfun-authors@google.com
|
8
|
-
License: Apache License 2.0
|
9
|
-
Keywords: llm generative-ai machine-learning
|
10
|
-
Classifier: Development Status :: 3 - Alpha
|
11
|
-
Classifier: Intended Audience :: Developers
|
12
|
-
Classifier: Intended Audience :: Education
|
13
|
-
Classifier: Intended Audience :: Science/Research
|
14
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
17
|
-
Classifier: Programming Language :: Python :: 3.11
|
18
|
-
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
19
|
-
Classifier: Topic :: Scientific/Engineering :: Human Machine Interfaces
|
20
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
21
|
-
Classifier: Topic :: Software Development :: Libraries
|
22
|
-
Description-Content-Type: text/markdown
|
23
|
-
License-File: LICENSE
|
24
|
-
Requires-Dist: google-cloud-aiplatform >=1.5.0
|
25
|
-
Requires-Dist: google-generativeai >=0.3.2
|
26
|
-
Requires-Dist: jinja2 >=3.1.2
|
27
|
-
Requires-Dist: openai ==0.27.2
|
28
|
-
Requires-Dist: openpyxl >=3.1.0
|
29
|
-
Requires-Dist: pandas >=2.1.4
|
30
|
-
Requires-Dist: pyglove >=0.4.5.dev20240423
|
31
|
-
Requires-Dist: python-docx >=0.8.11
|
32
|
-
Requires-Dist: python-magic >=0.4.27
|
33
|
-
Requires-Dist: requests >=2.31.0
|
34
|
-
Requires-Dist: termcolor ==1.1.0
|
35
|
-
Requires-Dist: tqdm >=4.64.1
|
36
|
-
|
37
|
-
<div align="center">
|
38
|
-
<img src="https://raw.githubusercontent.com/google/langfun/main/docs/_static/logo.svg" width="520px" alt="logo"></img>
|
39
|
-
</div>
|
40
|
-
|
41
|
-
# Langfun
|
42
|
-
|
43
|
-
[](https://badge.fury.io/py/langfun)
|
44
|
-
[](https://codecov.io/gh/google/langfun)
|
45
|
-

|
46
|
-
|
47
|
-
[**Installation**](#install) | [**Getting started**](#hello-world)
|
48
|
-
|
49
|
-
## What is Langfun
|
50
|
-
|
51
|
-
Langfun is a Python library that aims to make language models (LM) fun
|
52
|
-
to work with. Its design enables a programming model that flows naturally,
|
53
|
-
resembling the human thought process. It emphasizes the reuse and combination of
|
54
|
-
language pieces to form prompts, thereby accelerating innovation. In contrast to
|
55
|
-
other LM frameworks, which feed program-generated data into the LM, langfun
|
56
|
-
takes a distinct approach: It starts with natural language, allowing for
|
57
|
-
seamless interactions between language and program logic, and concludes with
|
58
|
-
natural language and optional structured output. Consequently, langfun can
|
59
|
-
aptly be described as Language as functions, capturing the core of its
|
60
|
-
methodology.
|
61
|
-
|
62
|
-
## Install
|
63
|
-
|
64
|
-
```
|
65
|
-
pip install langfun
|
66
|
-
```
|
67
|
-
|
68
|
-
Or install nightly build with:
|
69
|
-
|
70
|
-
```
|
71
|
-
pip install langfun --pre
|
72
|
-
```
|
73
|
-
|
74
|
-
## Hello World
|
75
|
-
|
76
|
-
```python
|
77
|
-
import langfun as lf
|
78
|
-
|
79
|
-
class NumericAnswerExtractor(lf.LangFunc):
|
80
|
-
"""Numeric answer extractor.
|
81
|
-
|
82
|
-
Here is my question:
|
83
|
-
{{question}}
|
84
|
-
|
85
|
-
Here is the response:
|
86
|
-
{{question()}}
|
87
|
-
|
88
|
-
Can you help me extract a number from the response as the answer to my
|
89
|
-
question? Your response should only contain a number in numeric form.
|
90
|
-
If the answer is not a number or you cannot extract it, respond with UNKNOWN.
|
91
|
-
"""
|
92
|
-
output_transform = lf.transforms.Match('\d+').to_int()
|
93
|
-
|
94
|
-
l = NumericAnswerExtractor()
|
95
|
-
|
96
|
-
with lf.context(lm=lf.llms.Gpt35(debug=True)):
|
97
|
-
r = l(question=lf.LangFunc('What is result of {{x}} plus {{y}}?'),
|
98
|
-
x='one',
|
99
|
-
y='two')
|
100
|
-
print('Result:', r.result)
|
101
|
-
```
|
102
|
-
|
103
|
-
*Disclaimer: this is not an officially supported Google product.*
|
File without changes
|
File without changes
|