langfun 0.0.2.dev20240717__tar.gz → 0.1.0__tar.gz

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.
Files changed (132) hide show
  1. langfun-0.1.0/PKG-INFO +168 -0
  2. langfun-0.1.0/README.md +131 -0
  3. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/__init__.py +1 -1
  4. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/language_model.py +7 -4
  5. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/image.py +6 -0
  6. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/image_test.py +6 -0
  7. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/prompting.py +5 -0
  8. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/prompting_test.py +11 -0
  9. langfun-0.1.0/langfun.egg-info/PKG-INFO +168 -0
  10. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun.egg-info/requires.txt +1 -0
  11. langfun-0.0.2.dev20240717/PKG-INFO +0 -103
  12. langfun-0.0.2.dev20240717/README.md +0 -67
  13. langfun-0.0.2.dev20240717/langfun.egg-info/PKG-INFO +0 -103
  14. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/LICENSE +0 -0
  15. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/__init__.py +0 -0
  16. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/__init__.py +0 -0
  17. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/__init__.py +0 -0
  18. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/correction.py +0 -0
  19. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/correction_test.py +0 -0
  20. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/errors.py +0 -0
  21. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/errors_test.py +0 -0
  22. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/execution.py +0 -0
  23. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/execution_test.py +0 -0
  24. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/generation.py +0 -0
  25. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/generation_test.py +0 -0
  26. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/parsing.py +0 -0
  27. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/parsing_test.py +0 -0
  28. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/permissions.py +0 -0
  29. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/coding/python/permissions_test.py +0 -0
  30. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/component.py +0 -0
  31. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/component_test.py +0 -0
  32. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/concurrent.py +0 -0
  33. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/concurrent_test.py +0 -0
  34. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/console.py +0 -0
  35. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/console_test.py +0 -0
  36. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/__init__.py +0 -0
  37. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/base.py +0 -0
  38. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/base_test.py +0 -0
  39. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/matching.py +0 -0
  40. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/matching_test.py +0 -0
  41. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/patching.py +0 -0
  42. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/patching_test.py +0 -0
  43. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/scoring.py +0 -0
  44. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/eval/scoring_test.py +0 -0
  45. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/langfunc.py +0 -0
  46. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/langfunc_test.py +0 -0
  47. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/language_model_test.py +0 -0
  48. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/__init__.py +0 -0
  49. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/anthropic.py +0 -0
  50. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/anthropic_test.py +0 -0
  51. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/cache/__init__.py +0 -0
  52. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/cache/base.py +0 -0
  53. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/cache/in_memory.py +0 -0
  54. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/cache/in_memory_test.py +0 -0
  55. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/fake.py +0 -0
  56. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/fake_test.py +0 -0
  57. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/google_genai.py +0 -0
  58. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/google_genai_test.py +0 -0
  59. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/groq.py +0 -0
  60. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/groq_test.py +0 -0
  61. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/llama_cpp.py +0 -0
  62. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/llama_cpp_test.py +0 -0
  63. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/openai.py +0 -0
  64. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/openai_test.py +0 -0
  65. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/rest.py +0 -0
  66. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/rest_test.py +0 -0
  67. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/vertexai.py +0 -0
  68. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/llms/vertexai_test.py +0 -0
  69. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/logging.py +0 -0
  70. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/logging_test.py +0 -0
  71. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/memories/__init__.py +0 -0
  72. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/memories/conversation_history.py +0 -0
  73. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/memories/conversation_history_test.py +0 -0
  74. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/memory.py +0 -0
  75. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/message.py +0 -0
  76. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/message_test.py +0 -0
  77. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/__init__.py +0 -0
  78. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/audio.py +0 -0
  79. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/audio_test.py +0 -0
  80. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/mime.py +0 -0
  81. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/mime_test.py +0 -0
  82. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/ms_office.py +0 -0
  83. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/ms_office_test.py +0 -0
  84. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/pdf.py +0 -0
  85. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/pdf_test.py +0 -0
  86. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/video.py +0 -0
  87. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modalities/video_test.py +0 -0
  88. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modality.py +0 -0
  89. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/modality_test.py +0 -0
  90. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/natural_language.py +0 -0
  91. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/natural_language_test.py +0 -0
  92. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/repr_utils.py +0 -0
  93. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/repr_utils_test.py +0 -0
  94. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/sampling.py +0 -0
  95. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/sampling_test.py +0 -0
  96. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/__init__.py +0 -0
  97. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/completion.py +0 -0
  98. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/completion_test.py +0 -0
  99. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/description.py +0 -0
  100. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/description_test.py +0 -0
  101. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/function_generation.py +0 -0
  102. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/function_generation_test.py +0 -0
  103. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/mapping.py +0 -0
  104. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/mapping_test.py +0 -0
  105. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/parsing.py +0 -0
  106. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/parsing_test.py +0 -0
  107. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/schema.py +0 -0
  108. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/schema_generation.py +0 -0
  109. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/schema_generation_test.py +0 -0
  110. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/schema_test.py +0 -0
  111. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/scoring.py +0 -0
  112. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/structured/scoring_test.py +0 -0
  113. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/subscription.py +0 -0
  114. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/subscription_test.py +0 -0
  115. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/template.py +0 -0
  116. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/template_test.py +0 -0
  117. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/__init__.py +0 -0
  118. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/completion.py +0 -0
  119. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/completion_test.py +0 -0
  120. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/conversation.py +0 -0
  121. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/conversation_test.py +0 -0
  122. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/demonstration.py +0 -0
  123. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/demonstration_test.py +0 -0
  124. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/selfplay.py +0 -0
  125. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/templates/selfplay_test.py +0 -0
  126. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/text_formatting.py +0 -0
  127. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun/core/text_formatting_test.py +0 -0
  128. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun.egg-info/SOURCES.txt +0 -0
  129. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun.egg-info/dependency_links.txt +0 -0
  130. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/langfun.egg-info/top_level.txt +0 -0
  131. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/setup.cfg +0 -0
  132. {langfun-0.0.2.dev20240717 → langfun-0.1.0}/setup.py +0 -0
langfun-0.1.0/PKG-INFO ADDED
@@ -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
+ [![PyPI version](https://badge.fury.io/py/langfun.svg)](https://badge.fury.io/py/langfun)
45
+ [![codecov](https://codecov.io/gh/google/langfun/branch/main/graph/badge.svg)](https://codecov.io/gh/google/langfun)
46
+ ![pytest](https://github.com/google/langfun/actions/workflows/ci.yaml/badge.svg)
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.*
@@ -0,0 +1,131 @@
1
+ <div align="center">
2
+ <img src="https://raw.githubusercontent.com/google/langfun/main/docs/_static/logo.svg" width="520px" alt="logo"></img>
3
+ </div>
4
+
5
+ # Langfun
6
+
7
+ [![PyPI version](https://badge.fury.io/py/langfun.svg)](https://badge.fury.io/py/langfun)
8
+ [![codecov](https://codecov.io/gh/google/langfun/branch/main/graph/badge.svg)](https://codecov.io/gh/google/langfun)
9
+ ![pytest](https://github.com/google/langfun/actions/workflows/ci.yaml/badge.svg)
10
+
11
+ [**Installation**](#install) | [**Getting started**](#hello-langfun) | [**Tutorial**](https://colab.research.google.com/github/google/langfun/blob/main/docs/notebooks/langfun101.ipynb)
12
+
13
+ ## Introduction
14
+
15
+ Langfun is a [PyGlove](https://github.com/google/pyglove) powered library that
16
+ aims to *make language models (LM) fun to work with*. Its central principle is
17
+ to enable seamless integration between natural language and programming by
18
+ treating language as functions. Through the introduction of *Object-Oriented Prompting*,
19
+ Langfun empowers users to prompt LLMs using objects and types, offering enhanced
20
+ control and simplifying agent development.
21
+
22
+ To unlock the magic of Langfun, you can start with
23
+ [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,
24
+ Claude, all without the need for additional fine-tuning.
25
+
26
+ ## Why Langfun?
27
+
28
+ Langfun is *powerful and scalable*:
29
+
30
+ * Seamless integration between natural language and computer programs.
31
+ * Modular prompts, which allows a natural blend of texts and modalities;
32
+ * Efficient for both request-based workflows and batch jobs;
33
+ * A powerful eval framework that thrives dimension explosions.
34
+
35
+ Langfun is *simple and elegant*:
36
+
37
+ * An intuitive programming model, graspable in 5 minutes;
38
+ * Plug-and-play into any Python codebase, making an immediate difference;
39
+ * Comprehensive LLMs under a unified API: Gemini, GPT, Claude, Llama3, and more.
40
+ * Designed for agile developement: offering intellisense, easy debugging, with minimal overhead;
41
+
42
+ ## Hello, Langfun
43
+
44
+ ```python
45
+ import langfun as lf
46
+ import pyglove as pg
47
+
48
+ from typing import Literal
49
+ from IPython import display
50
+
51
+ class Item(pg.Object):
52
+ name: str
53
+ size: Literal['large', 'medium', 'small']
54
+
55
+ class ImageDescription(pg.Object):
56
+ items: list[Item]
57
+
58
+ image = lf.Image.from_uri('https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Solar_system.jpg/1646px-Solar_system.jpg')
59
+ display.display(image)
60
+
61
+ desc = lf.query(
62
+ 'Describe objects in {{my_image}} from top to bottom.',
63
+ ImageDescription,
64
+ lm=lf.llms.Gpt4o(api_key='<your-openai-api-key>'),
65
+ my_image=image,
66
+ )
67
+ print(desc)
68
+ ```
69
+ *Output:*
70
+
71
+ <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Solar_system.jpg/1646px-Solar_system.jpg" width="520px" alt="my_image"></img>
72
+
73
+ ```
74
+ ImageDescription(
75
+ items = [
76
+ 0 : Item(
77
+ name = 'Mercury',
78
+ color = 'Gray'
79
+ ),
80
+ 1 : Item(
81
+ name = 'Venus',
82
+ color = 'Yellow'
83
+ ),
84
+ 2 : Item(
85
+ name = 'Earth',
86
+ color = 'Blue and white'
87
+ ),
88
+ 3 : Item(
89
+ name = 'Moon',
90
+ color = 'Gray'
91
+ ),
92
+ 4 : Item(
93
+ name = 'Mars',
94
+ color = 'Red'
95
+ ),
96
+ 5 : Item(
97
+ name = 'Jupiter',
98
+ color = 'Brown and white'
99
+ ),
100
+ 6 : Item(
101
+ name = 'Saturn',
102
+ color = 'Yellowish-brown with rings'
103
+ ),
104
+ 7 : Item(
105
+ name = 'Uranus',
106
+ color = 'Light blue'
107
+ ),
108
+ 8 : Item(
109
+ name = 'Neptune',
110
+ color = 'Dark blue'
111
+ )
112
+ ]
113
+ )
114
+ ```
115
+ See [Langfun 101](https://colab.research.google.com/github/google/langfun/blob/main/docs/notebooks/langfun101.ipynb) for more examples.
116
+
117
+ ## Install
118
+
119
+ ```
120
+ pip install langfun
121
+ ```
122
+
123
+ Or install nightly build with:
124
+
125
+ ```
126
+ pip install langfun --pre
127
+ ```
128
+
129
+
130
+
131
+ *Disclaimer: this is not an officially supported Google product.*
@@ -75,4 +75,4 @@ CodeError = coding.CodeError
75
75
  # pylint: enable=g-import-not-at-top
76
76
  # pylint: enable=g-bad-import-order
77
77
 
78
- __version__ = "0.0.2"
78
+ __version__ = "0.1.0"
@@ -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
- if not isinstance(usage, UsageNotAvailable) and model_id in self.usages:
739
- self.usages[model_id] += usage
740
- else:
741
- self.usages[model_id] = usage
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
@@ -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
+ [![PyPI version](https://badge.fury.io/py/langfun.svg)](https://badge.fury.io/py/langfun)
45
+ [![codecov](https://codecov.io/gh/google/langfun/branch/main/graph/badge.svg)](https://codecov.io/gh/google/langfun)
46
+ ![pytest](https://github.com/google/langfun/actions/workflows/ci.yaml/badge.svg)
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.*
@@ -10,3 +10,4 @@ python-magic>=0.4.27
10
10
  requests>=2.31.0
11
11
  termcolor==1.1.0
12
12
  tqdm>=4.64.1
13
+ pillow>=10.1.0
@@ -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
- [![PyPI version](https://badge.fury.io/py/langfun.svg)](https://badge.fury.io/py/langfun)
44
- [![codecov](https://codecov.io/gh/google/langfun/branch/main/graph/badge.svg)](https://codecov.io/gh/google/langfun)
45
- ![pytest](https://github.com/google/langfun/actions/workflows/ci.yaml/badge.svg)
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.*