Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions langfun/core/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
from langfun.core.llms.openai import OpenAI

from langfun.core.llms.openai import Gpt4Turbo
from langfun.core.llms.openai import Gpt4Turbo_0125
from langfun.core.llms.openai import Gpt4TurboVision
from langfun.core.llms.openai import Gpt4Turbo_20240409
from langfun.core.llms.openai import Gpt4TurboPreview
from langfun.core.llms.openai import Gpt4TurboPreview_0125
from langfun.core.llms.openai import Gpt4TurboPreview_1106
from langfun.core.llms.openai import Gpt4VisionPreview
from langfun.core.llms.openai import Gpt4VisionPreview_1106
from langfun.core.llms.openai import Gpt4
from langfun.core.llms.openai import Gpt4_0613
from langfun.core.llms.openai import Gpt4_32K
Expand Down
19 changes: 19 additions & 0 deletions langfun/core/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
'claude-3-opus-20240229': pg.Dict(max_tokens=4096, max_concurrency=1),
'claude-3-sonnet-20240229': pg.Dict(max_tokens=4096, max_concurrency=4),
'claude-3-haiku-20240307': pg.Dict(max_tokens=4096, max_concurrency=16),
'claude-2.1': pg.Dict(max_tokens=4096, max_concurrency=16),
'claude-2.0': pg.Dict(max_tokens=4096, max_concurrency=16),
'claude-instant-1.2': pg.Dict(max_tokens=4096, max_concurrency=16),
}


Expand Down Expand Up @@ -210,6 +213,7 @@ def _sample_single(self, prompt: lf.Message) -> lf.LMSamplingResult:

class Claude3(Anthropic):
"""Base class for Claude 3 models. 200K input tokens and 4K output tokens."""
multimodal = True


class Claude3Opus(Claude3):
Expand All @@ -228,3 +232,18 @@ class Claude3Haiku(Claude3):
"""Anthropic's most compact model."""

model = 'claude-3-haiku-20240307'


class Claude2(Anthropic):
"""Predecessor to Claude 3 with 100K context window.."""
model = 'claude-2.0'


class Claude21(Anthropic):
"""Updated Claude 2 model with improved accuracy and 200K context window."""
model = 'claude-2.1'


class ClaudeInstant(Anthropic):
"""Cheapest small and fast model, 100K context window."""
model = 'claude-instant-1.2'
46 changes: 33 additions & 13 deletions langfun/core/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
# The concurrent requests is estimated by TPM/RPM from
# https://platform.openai.com/account/limits
# GPT-4 Turbo models.
('gpt-4-turbo-preview', 1), # GPT-4 Turbo.
('gpt-4-0125-preview', 1), # GPT-4 Turbo
('gpt-4-1106-preview', 1), # GPT-4 Turbo
('gpt-4-vision-preview', 1), # GPT-4 Turbo with Vision.
('gpt-4-turbo', 8), # GPT-4 Turbo with Vision
('gpt-4-turbo-2024-04-09', 8), # GPT-4-Turbo with Vision, 04/09/2024
('gpt-4-turbo-preview', 8), # GPT-4 Turbo Preview
('gpt-4-0125-preview', 8), # GPT-4 Turbo Preview, 01/25/2024
('gpt-4-1106-preview', 8), # GPT-4 Turbo Preview, 11/06/2023
('gpt-4-vision-preview', 8), # GPT-4 Turbo Vision Preview.
('gpt-4-1106-vision-preview', 8), # GPT-4 Turbo Vision Preview, 11/06/2023
# GPT-4 models.
('gpt-4', 4),
('gpt-4-0613', 4),
Expand Down Expand Up @@ -284,26 +287,43 @@ class Gpt4(OpenAI):


class Gpt4Turbo(Gpt4):
"""GPT-4 Turbo with 128K context window size. Knowledge up to 4-2023."""
model = 'gpt-4-turbo-preview'
"""GPT-4 Turbo with 128K context window. Knowledge up to Dec. 2023."""
model = 'gpt-4-turbo'
multimodal = True


class Gpt4TurboVision(Gpt4Turbo):
"""GPT-4 Turbo with vision."""
model = 'gpt-4-vision-preview'
class Gpt4Turbo_20240409(Gpt4Turbo): # pylint:disable=invalid-name
"""GPT-4 Turbo with 128K context window. Knowledge up to Dec. 2023."""
model = 'gpt-4-turbo-2024-04-09'
multimodal = True


class Gpt4Turbo_0125(Gpt4Turbo): # pylint:disable=invalid-name
"""GPT-4 Turbo with 128K context window size. Knowledge up to 4-2023."""
class Gpt4TurboPreview(Gpt4):
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Dec. 2023."""
model = 'gpt-4-turbo-preview'


class Gpt4TurboPreview_0125(Gpt4TurboPreview): # pylint: disable=invalid-name
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Dec. 2023."""
model = 'gpt-4-0125-preview'


class Gpt4Turbo_1106(Gpt4Turbo): # pylint:disable=invalid-name
"""GPT-4 Turbo @20231106. 128K context window. Knowledge up to 4-2023."""
class Gpt4TurboPreview_1106(Gpt4TurboPreview): # pylint: disable=invalid-name
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Apr. 2023."""
model = 'gpt-4-1106-preview'


class Gpt4VisionPreview(Gpt4):
"""GPT-4 Turbo vision preview. 128k context window. Knowledge to Apr. 2023."""
model = 'gpt-4-vision-preview'
multimodal = True


class Gpt4VisionPreview_1106(Gpt4): # pylint: disable=invalid-name
"""GPT-4 Turbo vision preview. 128k context window. Knowledge to Apr. 2023."""
model = 'gpt-4-1106-vision-preview'


class Gpt4_0613(Gpt4): # pylint:disable=invalid-name
"""GPT-4 @20230613. 8K context window. Knowledge up to 9-2021."""
model = 'gpt-4-0613'
Expand Down
24 changes: 13 additions & 11 deletions langfun/core/llms/openai_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,19 @@ def test_call_chat_completion(self):
def test_call_chat_completion_vision(self):
with mock.patch('openai.ChatCompletion.create') as mock_chat_completion:
mock_chat_completion.side_effect = mock_chat_completion_query_vision
lm = openai.Gpt4TurboVision(api_key='test_key')
self.assertEqual(
lm(
lf.UserMessage(
'hello {{image}}',
image=lf_modalities.Image.from_uri('https://fake/image')
),
sampling_options=lf.LMSamplingOptions(n=2)
),
'Sample 0 for message: https://fake/image',
)
lm_1 = openai.Gpt4Turbo(api_key='test_key')
lm_2 = openai.Gpt4VisionPreview(api_key='test_key')
for lm in (lm_1, lm_2):
self.assertEqual(
lm(
lf.UserMessage(
'hello {{image}}',
image=lf_modalities.Image.from_uri('https://fake/image')
),
sampling_options=lf.LMSamplingOptions(n=2)
),
'Sample 0 for message: https://fake/image',
)

def test_sample_completion(self):
with mock.patch('openai.Completion.create') as mock_completion:
Expand Down