langfun 0.0.2.dev20240515__tar.gz → 0.0.2.dev20240517__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 (117) hide show
  1. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/PKG-INFO +1 -1
  2. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/language_model.py +25 -13
  3. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/language_model_test.py +16 -1
  4. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/fake.py +2 -1
  5. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/openai.py +11 -10
  6. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/vertexai.py +5 -5
  7. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/vertexai_test.py +6 -7
  8. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/scoring.py +22 -10
  9. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun.egg-info/PKG-INFO +1 -1
  10. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/LICENSE +0 -0
  11. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/README.md +0 -0
  12. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/__init__.py +0 -0
  13. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/__init__.py +0 -0
  14. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/__init__.py +0 -0
  15. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/__init__.py +0 -0
  16. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/correction.py +0 -0
  17. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/correction_test.py +0 -0
  18. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/errors.py +0 -0
  19. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/errors_test.py +0 -0
  20. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/execution.py +0 -0
  21. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/execution_test.py +0 -0
  22. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/generation.py +0 -0
  23. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/generation_test.py +0 -0
  24. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/parsing.py +0 -0
  25. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/parsing_test.py +0 -0
  26. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/permissions.py +0 -0
  27. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/coding/python/permissions_test.py +0 -0
  28. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/component.py +0 -0
  29. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/component_test.py +0 -0
  30. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/concurrent.py +0 -0
  31. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/concurrent_test.py +0 -0
  32. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/console.py +0 -0
  33. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/console_test.py +0 -0
  34. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/__init__.py +0 -0
  35. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/base.py +0 -0
  36. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/base_test.py +0 -0
  37. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/matching.py +0 -0
  38. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/matching_test.py +0 -0
  39. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/patching.py +0 -0
  40. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/patching_test.py +0 -0
  41. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/scoring.py +0 -0
  42. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/eval/scoring_test.py +0 -0
  43. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/langfunc.py +0 -0
  44. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/langfunc_test.py +0 -0
  45. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/__init__.py +0 -0
  46. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/anthropic.py +0 -0
  47. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/anthropic_test.py +0 -0
  48. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/cache/__init__.py +0 -0
  49. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/cache/base.py +0 -0
  50. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/cache/in_memory.py +0 -0
  51. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/cache/in_memory_test.py +0 -0
  52. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/fake_test.py +0 -0
  53. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/google_genai.py +0 -0
  54. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/google_genai_test.py +0 -0
  55. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/groq.py +0 -0
  56. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/groq_test.py +0 -0
  57. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/llama_cpp.py +0 -0
  58. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/llama_cpp_test.py +0 -0
  59. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/llms/openai_test.py +0 -0
  60. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/memories/__init__.py +0 -0
  61. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/memories/conversation_history.py +0 -0
  62. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/memories/conversation_history_test.py +0 -0
  63. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/memory.py +0 -0
  64. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/message.py +0 -0
  65. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/message_test.py +0 -0
  66. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/__init__.py +0 -0
  67. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/image.py +0 -0
  68. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/image_test.py +0 -0
  69. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/mime.py +0 -0
  70. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/mime_test.py +0 -0
  71. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/video.py +0 -0
  72. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modalities/video_test.py +0 -0
  73. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modality.py +0 -0
  74. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/modality_test.py +0 -0
  75. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/natural_language.py +0 -0
  76. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/natural_language_test.py +0 -0
  77. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/sampling.py +0 -0
  78. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/sampling_test.py +0 -0
  79. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/__init__.py +0 -0
  80. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/completion.py +0 -0
  81. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/completion_test.py +0 -0
  82. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/description.py +0 -0
  83. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/description_test.py +0 -0
  84. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/function_generation.py +0 -0
  85. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/function_generation_test.py +0 -0
  86. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/mapping.py +0 -0
  87. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/mapping_test.py +0 -0
  88. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/parsing.py +0 -0
  89. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/parsing_test.py +0 -0
  90. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/prompting.py +0 -0
  91. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/prompting_test.py +0 -0
  92. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/schema.py +0 -0
  93. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/schema_generation.py +0 -0
  94. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/schema_generation_test.py +0 -0
  95. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/schema_test.py +0 -0
  96. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/structured/scoring_test.py +0 -0
  97. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/subscription.py +0 -0
  98. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/subscription_test.py +0 -0
  99. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/template.py +0 -0
  100. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/template_test.py +0 -0
  101. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/__init__.py +0 -0
  102. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/completion.py +0 -0
  103. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/completion_test.py +0 -0
  104. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/conversation.py +0 -0
  105. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/conversation_test.py +0 -0
  106. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/demonstration.py +0 -0
  107. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/demonstration_test.py +0 -0
  108. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/selfplay.py +0 -0
  109. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/templates/selfplay_test.py +0 -0
  110. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/text_formatting.py +0 -0
  111. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun/core/text_formatting_test.py +0 -0
  112. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun.egg-info/SOURCES.txt +0 -0
  113. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun.egg-info/dependency_links.txt +0 -0
  114. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun.egg-info/requires.txt +0 -0
  115. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/langfun.egg-info/top_level.txt +0 -0
  116. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/setup.cfg +0 -0
  117. {langfun-0.0.2.dev20240515 → langfun-0.0.2.dev20240517}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240515
3
+ Version: 0.0.2.dev20240517
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -566,12 +566,19 @@ class LanguageModel(component.Component):
566
566
 
567
567
  def score(
568
568
  self,
569
- prompt: str | message_lib.Message,
569
+ prompt: str | message_lib.Message | list[message_lib.Message],
570
570
  completions: list[str | message_lib.Message],
571
571
  **kwargs,
572
572
  ) -> list[LMScoringResult]:
573
573
  """Scores the given prompt."""
574
- prompt = message_lib.UserMessage.from_value(prompt)
574
+ if isinstance(prompt, list):
575
+ if len(prompt) != len(completions):
576
+ raise ValueError(
577
+ 'prompt and completions must have the same length.'
578
+ )
579
+ prompt = [message_lib.UserMessage.from_value(p) for p in prompt]
580
+ else:
581
+ prompt = message_lib.UserMessage.from_value(prompt)
575
582
  completions = [message_lib.UserMessage.from_value(c) for c in completions]
576
583
 
577
584
  call_counter = self._call_counter
@@ -587,7 +594,8 @@ class LanguageModel(component.Component):
587
594
  return scoring_results
588
595
 
589
596
  def _score(
590
- self, prompt: message_lib.Message, completions: list[message_lib.Message]
597
+ self, prompt: message_lib.Message | list[message_lib.Message],
598
+ completions: list[message_lib.Message]
591
599
  ) -> list[LMScoringResult]:
592
600
  """Subclass to implement."""
593
601
  raise NotImplementedError(
@@ -596,7 +604,7 @@ class LanguageModel(component.Component):
596
604
 
597
605
  def _debug_score(
598
606
  self,
599
- prompt: message_lib.Message,
607
+ prompt: message_lib.Message | list[message_lib.Message],
600
608
  completions: list[message_lib.Message],
601
609
  scoring_results: list[LMScoringResult],
602
610
  call_counter: int,
@@ -615,15 +623,19 @@ class LanguageModel(component.Component):
615
623
  title=f'\n[{call_counter}] SCORING LM WITH PROMPT:',
616
624
  color='green',
617
625
  )
618
- referred_modalities = prompt.referred_modalities()
619
- if referred_modalities:
620
- console.write(
621
- pg.object_utils.kvlist_str(
622
- [(k, repr(v), None) for k, v in referred_modalities.items()]
623
- ),
624
- title=f'\n[{call_counter}] MODALITY OBJECTS SENT TO LM:',
625
- color='green',
626
- )
626
+ if isinstance(prompt, list):
627
+ referred_modalities_lst = [p.referred_modalities() for p in prompt]
628
+ else:
629
+ referred_modalities_lst = [prompt.referred_modalities(),]
630
+ if referred_modalities_lst:
631
+ for referred_modalities in referred_modalities_lst:
632
+ console.write(
633
+ pg.object_utils.kvlist_str(
634
+ [(k, repr(v), None) for k, v in referred_modalities.items()]
635
+ ),
636
+ title=f'\n[{call_counter}] MODALITY OBJECTS SENT TO LM:',
637
+ color='green',
638
+ )
627
639
 
628
640
  if debug & LMDebugMode.RESPONSE:
629
641
  console.write(
@@ -68,7 +68,7 @@ class MockScoringModel(MockModel):
68
68
 
69
69
  def _score(
70
70
  self,
71
- prompt: message_lib.Message,
71
+ prompt: message_lib.Message | list[message_lib.Message],
72
72
  completions: list[message_lib.Message],
73
73
  **kwargs
74
74
  ) -> list[lm_lib.LMScoringResult]:
@@ -508,6 +508,17 @@ class LanguageModelTest(unittest.TestCase):
508
508
  ],
509
509
  )
510
510
 
511
+ self.assertEqual(
512
+ lm.score(
513
+ [message_lib.UserMessage('hi {{image}}', image=Image()),
514
+ message_lib.UserMessage('hi {{image}}', image=Image())],
515
+ ['1', '2'], debug=debug_mode),
516
+ [
517
+ lm_lib.LMScoringResult(score=-0.0),
518
+ lm_lib.LMScoringResult(score=-1.0),
519
+ ],
520
+ )
521
+
511
522
  debug_info = string_io.getvalue()
512
523
  expected_included = [
513
524
  debug_prints[f]
@@ -528,6 +539,10 @@ class LanguageModelTest(unittest.TestCase):
528
539
  if debug_mode & lm_lib.LMDebugMode.PROMPT:
529
540
  self.assertIn('[0] MODALITY OBJECTS SENT TO LM', debug_info)
530
541
 
542
+ def test_score_with_unmatched_prompt_and_completions(self):
543
+ with self.assertRaises(ValueError):
544
+ MockScoringModel().score(['hi',], ['1', '2', '3'])
545
+
531
546
  def test_score_with_unsupported_model(self):
532
547
  with self.assertRaises(NotImplementedError):
533
548
  MockModel().score('hi', ['1', '2'])
@@ -21,7 +21,8 @@ import langfun.core as lf
21
21
  class Fake(lf.LanguageModel):
22
22
  """The base class for all fake language models."""
23
23
 
24
- def _score(self, prompt: lf.Message, completions: list[lf.Message]):
24
+ def _score(self, prompt: lf.Message| list[lf.Message],
25
+ completions: list[lf.Message]):
25
26
  return [lf.LMScoringResult(score=-i * 1.0) for i in range(len(completions))]
26
27
 
27
28
  def _sample(self, prompts: list[lf.Message]) -> list[lf.LMSamplingResult]:
@@ -33,17 +33,18 @@ _DEFAULT_RPM = 3000
33
33
  SUPPORTED_MODELS_AND_SETTINGS = {
34
34
  # Models from https://platform.openai.com/docs/models
35
35
  # RPM is from https://platform.openai.com/docs/guides/rate-limits
36
- 'gpt-4o': pg.Dict(rpm=10000, tpm=1500000),
37
- 'gpt-4o-2024-05-13': pg.Dict(rpm=10000, tpm=1500000),
38
- # GPT-4-Turbo models.
39
- 'gpt-4-turbo': pg.Dict(rpm=10000, tpm=1500000),
40
- 'gpt-4-turbo-2024-04-09': pg.Dict(rpm=10000, tpm=1500000),
41
- 'gpt-4-turbo-preview': pg.Dict(rpm=10000, tpm=1500000),
42
- 'gpt-4-0125-preview': pg.Dict(rpm=10000, tpm=1500000),
43
- 'gpt-4-1106-preview': pg.Dict(rpm=10000, tpm=1500000),
44
- 'gpt-4-vision-preview': pg.Dict(rpm=10000, tpm=1500000),
36
+ # GPT-4o models
37
+ 'gpt-4o': pg.Dict(rpm=10000, tpm=5000000),
38
+ 'gpt-4o-2024-05-13': pg.Dict(rpm=10000, tpm=5000000),
39
+ # GPT-4-Turbo models
40
+ 'gpt-4-turbo': pg.Dict(rpm=10000, tpm=2000000),
41
+ 'gpt-4-turbo-2024-04-09': pg.Dict(rpm=10000, tpm=2000000),
42
+ 'gpt-4-turbo-preview': pg.Dict(rpm=10000, tpm=2000000),
43
+ 'gpt-4-0125-preview': pg.Dict(rpm=10000, tpm=2000000),
44
+ 'gpt-4-1106-preview': pg.Dict(rpm=10000, tpm=2000000),
45
+ 'gpt-4-vision-preview': pg.Dict(rpm=10000, tpm=2000000),
45
46
  'gpt-4-1106-vision-preview': pg.Dict(
46
- rpm=10000, tpm=1500000
47
+ rpm=10000, tpm=2000000
47
48
  ),
48
49
  # GPT-4 models
49
50
  'gpt-4': pg.Dict(rpm=10000, tpm=300000),
@@ -99,7 +99,7 @@ class VertexAI(lf.LanguageModel):
99
99
 
100
100
  credentials = self.credentials
101
101
  # Placeholder for Google-internal credentials.
102
- from google.cloud.aiplatform import vertexai # pylint: disable=g-import-not-at-top
102
+ import vertexai
103
103
  vertexai.init(project=project, location=location, credentials=credentials)
104
104
  return True
105
105
 
@@ -125,7 +125,7 @@ class VertexAI(lf.LanguageModel):
125
125
  self, options: lf.LMSamplingOptions
126
126
  ) -> Any: # generative_models.GenerationConfig
127
127
  """Creates generation config from langfun sampling options."""
128
- from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
128
+ from vertexai import generative_models
129
129
  return generative_models.GenerationConfig(
130
130
  temperature=options.temperature,
131
131
  top_p=options.top_p,
@@ -138,7 +138,7 @@ class VertexAI(lf.LanguageModel):
138
138
  self, prompt: lf.Message
139
139
  ) -> list[str | Any]:
140
140
  """Gets generation input from langfun message."""
141
- from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
141
+ from vertexai import generative_models
142
142
  chunks = []
143
143
  for lf_chunk in prompt.chunk():
144
144
  if isinstance(lf_chunk, str):
@@ -239,7 +239,7 @@ class _ModelHub:
239
239
  """Gets a generative model by model id."""
240
240
  model = self._generative_model_cache.get(model_id, None)
241
241
  if model is None:
242
- from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
242
+ from vertexai import generative_models
243
243
  model = generative_models.GenerativeModel(model_id)
244
244
  self._generative_model_cache[model_id] = model
245
245
  return model
@@ -250,7 +250,7 @@ class _ModelHub:
250
250
  """Gets a text generation model by model id."""
251
251
  model = self._text_generation_model_cache.get(model_id, None)
252
252
  if model is None:
253
- from google.cloud.aiplatform.vertexai import language_models # pylint: disable=g-import-not-at-top
253
+ from vertexai import language_models
254
254
  model = language_models.TextGenerationModel.from_pretrained(model_id)
255
255
  self._text_generation_model_cache[model_id] = model
256
256
  return model
@@ -17,7 +17,7 @@ import os
17
17
  import unittest
18
18
  from unittest import mock
19
19
 
20
- from google.cloud.aiplatform.vertexai.preview import generative_models
20
+ from vertexai import generative_models
21
21
  import langfun.core as lf
22
22
  from langfun.core import modalities as lf_modalities
23
23
  from langfun.core.llms import vertexai
@@ -39,7 +39,6 @@ example_image = (
39
39
  def mock_generate_content(content, generation_config, **kwargs):
40
40
  del kwargs
41
41
  c = pg.Dict(generation_config.to_dict())
42
- print('zzz', c)
43
42
  return generative_models.GenerationResponse.from_dict({
44
43
  'candidates': [
45
44
  {
@@ -111,7 +110,7 @@ class VertexAITest(unittest.TestCase):
111
110
 
112
111
  def test_model_hub(self):
113
112
  with mock.patch(
114
- 'google.cloud.aiplatform.vertexai.preview.generative_models.'
113
+ 'vertexai.generative_models.'
115
114
  'GenerativeModel.__init__'
116
115
  ) as mock_model_init:
117
116
  mock_model_init.side_effect = lambda *args, **kwargs: None
@@ -125,7 +124,7 @@ class VertexAITest(unittest.TestCase):
125
124
  )
126
125
 
127
126
  with mock.patch(
128
- 'google.cloud.aiplatform.vertexai.language_models.'
127
+ 'vertexai.language_models.'
129
128
  'TextGenerationModel.from_pretrained'
130
129
  ) as mock_model_init:
131
130
 
@@ -163,13 +162,13 @@ class VertexAITest(unittest.TestCase):
163
162
 
164
163
  def test_call_generative_model(self):
165
164
  with mock.patch(
166
- 'google.cloud.aiplatform.vertexai.preview.generative_models.'
165
+ 'vertexai.generative_models.'
167
166
  'GenerativeModel.__init__'
168
167
  ) as mock_model_init:
169
168
  mock_model_init.side_effect = lambda *args, **kwargs: None
170
169
 
171
170
  with mock.patch(
172
- 'google.cloud.aiplatform.vertexai.preview.generative_models.'
171
+ 'vertexai.generative_models.'
173
172
  'GenerativeModel.generate_content'
174
173
  ) as mock_generate:
175
174
  mock_generate.side_effect = mock_generate_content
@@ -192,7 +191,7 @@ class VertexAITest(unittest.TestCase):
192
191
 
193
192
  def test_call_text_generation_model(self):
194
193
  with mock.patch(
195
- 'google.cloud.aiplatform.vertexai.language_models.'
194
+ 'vertexai.language_models.'
196
195
  'TextGenerationModel.from_pretrained'
197
196
  ) as mock_model_init:
198
197
 
@@ -23,7 +23,7 @@ import pyglove as pg
23
23
 
24
24
 
25
25
  def score(
26
- prompt: Union[str, pg.Symbolic],
26
+ prompt: Union[str, pg.Symbolic] | list[str | pg.Symbolic],
27
27
  completions: list[str | pg.Symbolic],
28
28
  schema: Union[
29
29
  schema_lib.Schema, Type[Any], list[Type[Any]], dict[str, Any], None
@@ -49,15 +49,27 @@ def score(
49
49
  f'{[type(c) for c in completions]}.'
50
50
  )
51
51
 
52
- input_message = prompting.query(
53
- prompt,
54
- schema,
55
- examples=examples,
56
- protocol=protocol,
57
- skip_lm=True,
58
- returns_message=True,
59
- **kwargs,
60
- )
52
+ if isinstance(prompt, list):
53
+ prompts = []
54
+ for p in prompt:
55
+ prompts.append(
56
+ prompting.query_prompt(
57
+ p,
58
+ schema,
59
+ examples=examples,
60
+ protocol=protocol,
61
+ **kwargs,
62
+ )
63
+ )
64
+ input_message = prompts
65
+ else:
66
+ input_message = prompting.query_prompt(
67
+ prompt,
68
+ schema,
69
+ examples=examples,
70
+ protocol=protocol,
71
+ **kwargs,
72
+ )
61
73
  if lm is None:
62
74
  lm_override = lf.get_contextual_override('lm')
63
75
  if lm_override is None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240515
3
+ Version: 0.0.2.dev20240517
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors