airtrain 0.1.12__py3-none-any.whl → 0.1.13__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.
- airtrain/__init__.py +1 -1
- airtrain/contrib/travel/__init__.py +5 -5
- airtrain/integrations/openai/models_config.py +119 -0
- airtrain/integrations/together/audio_models_config.py +34 -0
- airtrain/integrations/together/credentials.py +3 -3
- airtrain/integrations/together/embedding_models_config.py +92 -0
- airtrain/integrations/together/image_models_config.py +69 -0
- airtrain/integrations/together/image_skill.py +171 -0
- airtrain/integrations/together/models.py +56 -0
- airtrain/integrations/together/models_config.py +277 -0
- airtrain/integrations/together/rerank_models_config.py +43 -0
- airtrain/integrations/together/rerank_skill.py +49 -0
- airtrain/integrations/together/schemas.py +33 -0
- airtrain/integrations/together/skills.py +87 -1
- airtrain/integrations/together/vision_models_config.py +49 -0
- {airtrain-0.1.12.dist-info → airtrain-0.1.13.dist-info}/METADATA +1 -1
- {airtrain-0.1.12.dist-info → airtrain-0.1.13.dist-info}/RECORD +19 -8
- {airtrain-0.1.12.dist-info → airtrain-0.1.13.dist-info}/WHEEL +0 -0
- {airtrain-0.1.12.dist-info → airtrain-0.1.13.dist-info}/top_level.txt +0 -0
airtrain/__init__.py
CHANGED
@@ -4,9 +4,9 @@ from .agents import (
|
|
4
4
|
TravelAgentBase,
|
5
5
|
ClothingAgent,
|
6
6
|
HikingAgent,
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
InternetAgent,
|
8
|
+
FoodAgent,
|
9
|
+
PersonalizedAgent,
|
10
10
|
)
|
11
11
|
from .models import (
|
12
12
|
ClothingRecommendation,
|
@@ -14,8 +14,8 @@ from .models import (
|
|
14
14
|
InternetAvailability,
|
15
15
|
FoodOption,
|
16
16
|
)
|
17
|
-
from .
|
18
|
-
from .
|
17
|
+
from .agentlib.verification_agent import UserVerificationAgent
|
18
|
+
from .modellib.verification import UserTravelInfo, TravelCompanion, HealthCondition
|
19
19
|
|
20
20
|
__all__ = [
|
21
21
|
"TravelAgentBase",
|
@@ -0,0 +1,119 @@
|
|
1
|
+
from typing import Dict, NamedTuple, Optional
|
2
|
+
from decimal import Decimal
|
3
|
+
|
4
|
+
|
5
|
+
class OpenAIModelConfig(NamedTuple):
|
6
|
+
display_name: str
|
7
|
+
base_model: str
|
8
|
+
input_price: Decimal
|
9
|
+
cached_input_price: Optional[Decimal]
|
10
|
+
output_price: Decimal
|
11
|
+
|
12
|
+
|
13
|
+
OPENAI_MODELS: Dict[str, OpenAIModelConfig] = {
|
14
|
+
"gpt-4o": OpenAIModelConfig(
|
15
|
+
display_name="GPT-4 Optimized",
|
16
|
+
base_model="gpt-4o",
|
17
|
+
input_price=Decimal("2.50"),
|
18
|
+
cached_input_price=Decimal("1.25"),
|
19
|
+
output_price=Decimal("10.00"),
|
20
|
+
),
|
21
|
+
"gpt-4o-2024-08-06": OpenAIModelConfig(
|
22
|
+
display_name="GPT-4 Optimized (2024-08-06)",
|
23
|
+
base_model="gpt-4o",
|
24
|
+
input_price=Decimal("2.50"),
|
25
|
+
cached_input_price=Decimal("1.25"),
|
26
|
+
output_price=Decimal("10.00"),
|
27
|
+
),
|
28
|
+
"gpt-4o-2024-05-13": OpenAIModelConfig(
|
29
|
+
display_name="GPT-4 Optimized (2024-05-13)",
|
30
|
+
base_model="gpt-4o",
|
31
|
+
input_price=Decimal("5.00"),
|
32
|
+
cached_input_price=None,
|
33
|
+
output_price=Decimal("15.00"),
|
34
|
+
),
|
35
|
+
"gpt-4o-audio-preview-2024-12-17": OpenAIModelConfig(
|
36
|
+
display_name="GPT-4 Optimized Audio Preview",
|
37
|
+
base_model="gpt-4o-audio-preview",
|
38
|
+
input_price=Decimal("2.50"),
|
39
|
+
cached_input_price=None,
|
40
|
+
output_price=Decimal("10.00"),
|
41
|
+
),
|
42
|
+
"gpt-4o-realtime-preview-2024-12-17": OpenAIModelConfig(
|
43
|
+
display_name="GPT-4 Optimized Realtime Preview",
|
44
|
+
base_model="gpt-4o-realtime-preview",
|
45
|
+
input_price=Decimal("5.00"),
|
46
|
+
cached_input_price=Decimal("2.50"),
|
47
|
+
output_price=Decimal("20.00"),
|
48
|
+
),
|
49
|
+
"gpt-4o-mini-2024-07-18": OpenAIModelConfig(
|
50
|
+
display_name="GPT-4 Optimized Mini",
|
51
|
+
base_model="gpt-4o-mini",
|
52
|
+
input_price=Decimal("0.15"),
|
53
|
+
cached_input_price=Decimal("0.075"),
|
54
|
+
output_price=Decimal("0.60"),
|
55
|
+
),
|
56
|
+
"gpt-4o-mini-audio-preview-2024-12-17": OpenAIModelConfig(
|
57
|
+
display_name="GPT-4 Optimized Mini Audio Preview",
|
58
|
+
base_model="gpt-4o-mini-audio-preview",
|
59
|
+
input_price=Decimal("0.15"),
|
60
|
+
cached_input_price=None,
|
61
|
+
output_price=Decimal("0.60"),
|
62
|
+
),
|
63
|
+
"gpt-4o-mini-realtime-preview-2024-12-17": OpenAIModelConfig(
|
64
|
+
display_name="GPT-4 Optimized Mini Realtime Preview",
|
65
|
+
base_model="gpt-4o-mini-realtime-preview",
|
66
|
+
input_price=Decimal("0.60"),
|
67
|
+
cached_input_price=Decimal("0.30"),
|
68
|
+
output_price=Decimal("2.40"),
|
69
|
+
),
|
70
|
+
"o1-2024-12-17": OpenAIModelConfig(
|
71
|
+
display_name="O1",
|
72
|
+
base_model="o1",
|
73
|
+
input_price=Decimal("15.00"),
|
74
|
+
cached_input_price=Decimal("7.50"),
|
75
|
+
output_price=Decimal("60.00"),
|
76
|
+
),
|
77
|
+
"o3-mini-2025-01-31": OpenAIModelConfig(
|
78
|
+
display_name="O3 Mini",
|
79
|
+
base_model="o3-mini",
|
80
|
+
input_price=Decimal("1.10"),
|
81
|
+
cached_input_price=Decimal("0.55"),
|
82
|
+
output_price=Decimal("4.40"),
|
83
|
+
),
|
84
|
+
"o1-mini-2024-09-12": OpenAIModelConfig(
|
85
|
+
display_name="O1 Mini",
|
86
|
+
base_model="o1-mini",
|
87
|
+
input_price=Decimal("1.10"),
|
88
|
+
cached_input_price=Decimal("0.55"),
|
89
|
+
output_price=Decimal("4.40"),
|
90
|
+
),
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
def get_model_config(model_id: str) -> OpenAIModelConfig:
|
95
|
+
"""Get model configuration by model ID"""
|
96
|
+
if model_id not in OPENAI_MODELS:
|
97
|
+
raise ValueError(f"Model {model_id} not found in OpenAI models")
|
98
|
+
return OPENAI_MODELS[model_id]
|
99
|
+
|
100
|
+
|
101
|
+
def get_default_model() -> str:
|
102
|
+
"""Get the default model ID"""
|
103
|
+
return "gpt-4o"
|
104
|
+
|
105
|
+
|
106
|
+
def calculate_cost(
|
107
|
+
model_id: str, input_tokens: int, output_tokens: int, use_cached: bool = False
|
108
|
+
) -> Decimal:
|
109
|
+
"""Calculate cost for token usage"""
|
110
|
+
config = get_model_config(model_id)
|
111
|
+
input_price = (
|
112
|
+
config.cached_input_price
|
113
|
+
if (use_cached and config.cached_input_price is not None)
|
114
|
+
else config.input_price
|
115
|
+
)
|
116
|
+
return (
|
117
|
+
input_price * Decimal(str(input_tokens))
|
118
|
+
+ config.output_price * Decimal(str(output_tokens))
|
119
|
+
) / Decimal("1000")
|
@@ -0,0 +1,34 @@
|
|
1
|
+
from typing import Dict, NamedTuple
|
2
|
+
|
3
|
+
|
4
|
+
class AudioModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
|
8
|
+
|
9
|
+
TOGETHER_AUDIO_MODELS: Dict[str, AudioModelConfig] = {
|
10
|
+
"Cartesia/Sonic": AudioModelConfig(
|
11
|
+
organization="Cartesia", display_name="Cartesia/Sonic"
|
12
|
+
)
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
def get_audio_model_config(model_id: str) -> AudioModelConfig:
|
17
|
+
"""Get audio model configuration by model ID"""
|
18
|
+
if model_id not in TOGETHER_AUDIO_MODELS:
|
19
|
+
raise ValueError(f"Model {model_id} not found in Together AI audio models")
|
20
|
+
return TOGETHER_AUDIO_MODELS[model_id]
|
21
|
+
|
22
|
+
|
23
|
+
def list_audio_models_by_organization(organization: str) -> Dict[str, AudioModelConfig]:
|
24
|
+
"""Get all audio models for a specific organization"""
|
25
|
+
return {
|
26
|
+
model_id: config
|
27
|
+
for model_id, config in TOGETHER_AUDIO_MODELS.items()
|
28
|
+
if config.organization.lower() == organization.lower()
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
def get_default_audio_model() -> str:
|
33
|
+
"""Get the default audio model ID"""
|
34
|
+
return "Cartesia/Sonic"
|
@@ -6,14 +6,14 @@ import together
|
|
6
6
|
class TogetherAICredentials(BaseCredentials):
|
7
7
|
"""Together AI credentials"""
|
8
8
|
|
9
|
-
|
9
|
+
together_api_key: SecretStr = Field(..., description="Together AI API key")
|
10
10
|
|
11
|
-
_required_credentials = {"
|
11
|
+
_required_credentials = {"together_api_key"}
|
12
12
|
|
13
13
|
async def validate_credentials(self) -> bool:
|
14
14
|
"""Validate Together AI credentials"""
|
15
15
|
try:
|
16
|
-
together.api_key = self.
|
16
|
+
together.api_key = self.together_api_key.get_secret_value()
|
17
17
|
await together.Models.list()
|
18
18
|
return True
|
19
19
|
except Exception as e:
|
@@ -0,0 +1,92 @@
|
|
1
|
+
from typing import Dict, NamedTuple
|
2
|
+
|
3
|
+
|
4
|
+
class EmbeddingModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
model_size: str
|
8
|
+
embedding_dimension: int
|
9
|
+
context_window: int
|
10
|
+
|
11
|
+
|
12
|
+
TOGETHER_EMBEDDING_MODELS: Dict[str, EmbeddingModelConfig] = {
|
13
|
+
"togethercomputer/m2-bert-80M-2k-retrieval": EmbeddingModelConfig(
|
14
|
+
organization="Together",
|
15
|
+
display_name="M2-BERT-80M-2K-Retrieval",
|
16
|
+
model_size="80M",
|
17
|
+
embedding_dimension=768,
|
18
|
+
context_window=2048,
|
19
|
+
),
|
20
|
+
"togethercomputer/m2-bert-80M-8k-retrieval": EmbeddingModelConfig(
|
21
|
+
organization="Together",
|
22
|
+
display_name="M2-BERT-80M-8K-Retrieval",
|
23
|
+
model_size="80M",
|
24
|
+
embedding_dimension=768,
|
25
|
+
context_window=8192,
|
26
|
+
),
|
27
|
+
"togethercomputer/m2-bert-80M-32k-retrieval": EmbeddingModelConfig(
|
28
|
+
organization="Together",
|
29
|
+
display_name="M2-BERT-80M-32K-Retrieval",
|
30
|
+
model_size="80M",
|
31
|
+
embedding_dimension=768,
|
32
|
+
context_window=32768,
|
33
|
+
),
|
34
|
+
"WhereIsAI/UAE-Large-V1": EmbeddingModelConfig(
|
35
|
+
organization="WhereIsAI",
|
36
|
+
display_name="UAE-Large-v1",
|
37
|
+
model_size="326M",
|
38
|
+
embedding_dimension=1024,
|
39
|
+
context_window=512,
|
40
|
+
),
|
41
|
+
"BAAI/bge-large-en-v1.5": EmbeddingModelConfig(
|
42
|
+
organization="BAAI",
|
43
|
+
display_name="BGE-Large-EN-v1.5",
|
44
|
+
model_size="326M",
|
45
|
+
embedding_dimension=1024,
|
46
|
+
context_window=512,
|
47
|
+
),
|
48
|
+
"BAAI/bge-base-en-v1.5": EmbeddingModelConfig(
|
49
|
+
organization="BAAI",
|
50
|
+
display_name="BGE-Base-EN-v1.5",
|
51
|
+
model_size="102M",
|
52
|
+
embedding_dimension=768,
|
53
|
+
context_window=512,
|
54
|
+
),
|
55
|
+
"sentence-transformers/msmarco-bert-base-dot-v5": EmbeddingModelConfig(
|
56
|
+
organization="sentence-transformers",
|
57
|
+
display_name="Sentence-BERT",
|
58
|
+
model_size="110M",
|
59
|
+
embedding_dimension=768,
|
60
|
+
context_window=512,
|
61
|
+
),
|
62
|
+
"bert-base-uncased": EmbeddingModelConfig(
|
63
|
+
organization="Hugging Face",
|
64
|
+
display_name="BERT",
|
65
|
+
model_size="110M",
|
66
|
+
embedding_dimension=768,
|
67
|
+
context_window=512,
|
68
|
+
),
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
def get_embedding_model_config(model_id: str) -> EmbeddingModelConfig:
|
73
|
+
"""Get embedding model configuration by model ID"""
|
74
|
+
if model_id not in TOGETHER_EMBEDDING_MODELS:
|
75
|
+
raise ValueError(f"Model {model_id} not found in Together AI embedding models")
|
76
|
+
return TOGETHER_EMBEDDING_MODELS[model_id]
|
77
|
+
|
78
|
+
|
79
|
+
def list_embedding_models_by_organization(
|
80
|
+
organization: str,
|
81
|
+
) -> Dict[str, EmbeddingModelConfig]:
|
82
|
+
"""Get all embedding models for a specific organization"""
|
83
|
+
return {
|
84
|
+
model_id: config
|
85
|
+
for model_id, config in TOGETHER_EMBEDDING_MODELS.items()
|
86
|
+
if config.organization.lower() == organization.lower()
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
def get_default_embedding_model() -> str:
|
91
|
+
"""Get the default embedding model ID"""
|
92
|
+
return "togethercomputer/m2-bert-80M-32k-retrieval"
|
@@ -0,0 +1,69 @@
|
|
1
|
+
from typing import Dict, NamedTuple, Optional
|
2
|
+
|
3
|
+
|
4
|
+
class ImageModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
default_steps: Optional[int]
|
8
|
+
|
9
|
+
|
10
|
+
TOGETHER_IMAGE_MODELS: Dict[str, ImageModelConfig] = {
|
11
|
+
"black-forest-labs/FLUX.1-schnell-Free": ImageModelConfig(
|
12
|
+
organization="Black Forest Labs",
|
13
|
+
display_name="Flux.1 [schnell] (free)",
|
14
|
+
default_steps=None,
|
15
|
+
),
|
16
|
+
"black-forest-labs/FLUX.1-schnell": ImageModelConfig(
|
17
|
+
organization="Black Forest Labs",
|
18
|
+
display_name="Flux.1 [schnell] (Turbo)",
|
19
|
+
default_steps=4,
|
20
|
+
),
|
21
|
+
"black-forest-labs/FLUX.1-dev": ImageModelConfig(
|
22
|
+
organization="Black Forest Labs", display_name="Flux.1 Dev", default_steps=28
|
23
|
+
),
|
24
|
+
"black-forest-labs/FLUX.1-canny": ImageModelConfig(
|
25
|
+
organization="Black Forest Labs", display_name="Flux.1 Canny", default_steps=28
|
26
|
+
),
|
27
|
+
"black-forest-labs/FLUX.1-depth": ImageModelConfig(
|
28
|
+
organization="Black Forest Labs", display_name="Flux.1 Depth", default_steps=28
|
29
|
+
),
|
30
|
+
"black-forest-labs/FLUX.1-redux": ImageModelConfig(
|
31
|
+
organization="Black Forest Labs", display_name="Flux.1 Redux", default_steps=28
|
32
|
+
),
|
33
|
+
"black-forest-labs/FLUX.1.1-pro": ImageModelConfig(
|
34
|
+
organization="Black Forest Labs",
|
35
|
+
display_name="Flux1.1 [pro]",
|
36
|
+
default_steps=None,
|
37
|
+
),
|
38
|
+
"black-forest-labs/FLUX.1-pro": ImageModelConfig(
|
39
|
+
organization="Black Forest Labs",
|
40
|
+
display_name="Flux.1 [pro]",
|
41
|
+
default_steps=None,
|
42
|
+
),
|
43
|
+
"stabilityai/stable-diffusion-xl-base-1.0": ImageModelConfig(
|
44
|
+
organization="Stability AI",
|
45
|
+
display_name="Stable Diffusion XL 1.0",
|
46
|
+
default_steps=None,
|
47
|
+
),
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
def get_image_model_config(model_id: str) -> ImageModelConfig:
|
52
|
+
"""Get image model configuration by model ID"""
|
53
|
+
if model_id not in TOGETHER_IMAGE_MODELS:
|
54
|
+
raise ValueError(f"Model {model_id} not found in Together AI image models")
|
55
|
+
return TOGETHER_IMAGE_MODELS[model_id]
|
56
|
+
|
57
|
+
|
58
|
+
def list_image_models_by_organization(organization: str) -> Dict[str, ImageModelConfig]:
|
59
|
+
"""Get all image models for a specific organization"""
|
60
|
+
return {
|
61
|
+
model_id: config
|
62
|
+
for model_id, config in TOGETHER_IMAGE_MODELS.items()
|
63
|
+
if config.organization.lower() == organization.lower()
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
def get_default_image_model() -> str:
|
68
|
+
"""Get the default image model ID"""
|
69
|
+
return "black-forest-labs/FLUX.1-schnell-Free"
|
@@ -0,0 +1,171 @@
|
|
1
|
+
from typing import Optional, List
|
2
|
+
from pathlib import Path
|
3
|
+
from pydantic import Field
|
4
|
+
from together import Together
|
5
|
+
import base64
|
6
|
+
import time
|
7
|
+
|
8
|
+
from airtrain.core.skills import Skill, ProcessingError
|
9
|
+
from airtrain.core.schemas import InputSchema, OutputSchema
|
10
|
+
from .credentials import TogetherAICredentials
|
11
|
+
from .image_models_config import get_image_model_config, get_default_image_model
|
12
|
+
|
13
|
+
|
14
|
+
class TogetherAIImageInput(InputSchema):
|
15
|
+
"""Schema for Together AI image generation input"""
|
16
|
+
|
17
|
+
prompt: str = Field(..., description="Text prompt for image generation")
|
18
|
+
model: str = Field(
|
19
|
+
default=get_default_image_model(), description="Together AI image model to use"
|
20
|
+
)
|
21
|
+
steps: int = Field(default=10, description="Number of inference steps", ge=1, le=50)
|
22
|
+
n: int = Field(default=1, description="Number of images to generate", ge=1, le=4)
|
23
|
+
size: str = Field(
|
24
|
+
default="1024x1024", description="Image size in format WIDTHxHEIGHT"
|
25
|
+
)
|
26
|
+
negative_prompt: Optional[str] = Field(
|
27
|
+
default=None, description="Things to exclude from the generation"
|
28
|
+
)
|
29
|
+
seed: Optional[int] = Field(
|
30
|
+
default=None, description="Random seed for reproducibility"
|
31
|
+
)
|
32
|
+
|
33
|
+
|
34
|
+
class GeneratedImage(OutputSchema):
|
35
|
+
"""Individual generated image data"""
|
36
|
+
|
37
|
+
b64_json: str = Field(..., description="Base64 encoded image data")
|
38
|
+
seed: Optional[int] = Field(None, description="Seed used for this image")
|
39
|
+
finish_reason: Optional[str] = Field(
|
40
|
+
None, description="Reason for finishing generation"
|
41
|
+
)
|
42
|
+
url: Optional[str] = Field(None, description="URL of the generated image")
|
43
|
+
|
44
|
+
|
45
|
+
class TogetherAIImageOutput(OutputSchema):
|
46
|
+
"""Schema for Together AI image generation output"""
|
47
|
+
|
48
|
+
images: List[GeneratedImage] = Field(..., description="List of generated images")
|
49
|
+
model: str = Field(..., description="Model used for generation")
|
50
|
+
prompt: str = Field(..., description="Original prompt used")
|
51
|
+
total_time: float = Field(..., description="Time taken for generation in seconds")
|
52
|
+
usage: dict = Field(default_factory=dict, description="Usage statistics")
|
53
|
+
|
54
|
+
|
55
|
+
class TogetherAIImageSkill(Skill[TogetherAIImageInput, TogetherAIImageOutput]):
|
56
|
+
"""Skill for generating images using Together AI"""
|
57
|
+
|
58
|
+
input_schema = TogetherAIImageInput
|
59
|
+
output_schema = TogetherAIImageOutput
|
60
|
+
|
61
|
+
def __init__(self, credentials: Optional[TogetherAICredentials] = None):
|
62
|
+
"""Initialize the skill with optional credentials"""
|
63
|
+
super().__init__()
|
64
|
+
self.credentials = credentials or TogetherAICredentials.from_env()
|
65
|
+
self.client = Together(
|
66
|
+
api_key=self.credentials.together_api_key.get_secret_value()
|
67
|
+
)
|
68
|
+
|
69
|
+
def process(self, input_data: TogetherAIImageInput) -> TogetherAIImageOutput:
|
70
|
+
try:
|
71
|
+
# Validate the model exists in our config
|
72
|
+
get_image_model_config(input_data.model)
|
73
|
+
|
74
|
+
start_time = time.time()
|
75
|
+
|
76
|
+
# Generate images
|
77
|
+
response = self.client.images.generate(
|
78
|
+
prompt=input_data.prompt,
|
79
|
+
model=input_data.model,
|
80
|
+
steps=input_data.steps,
|
81
|
+
n=input_data.n,
|
82
|
+
size=input_data.size,
|
83
|
+
negative_prompt=input_data.negative_prompt,
|
84
|
+
seed=input_data.seed,
|
85
|
+
)
|
86
|
+
|
87
|
+
# Calculate total time
|
88
|
+
total_time = time.time() - start_time
|
89
|
+
|
90
|
+
# Debug print the response structure
|
91
|
+
print(f"Response type: {type(response)}")
|
92
|
+
print(f"Response data type: {type(response.data)}")
|
93
|
+
if response.data:
|
94
|
+
print(f"First image type: {type(response.data[0])}")
|
95
|
+
print(f"First image attributes: {dir(response.data[0])}")
|
96
|
+
|
97
|
+
# Convert response to our output format
|
98
|
+
generated_images = []
|
99
|
+
for img in response.data:
|
100
|
+
# Try different possible attribute names for image data
|
101
|
+
b64_data = None
|
102
|
+
for attr in ["b64_json", "image", "base64", "data"]:
|
103
|
+
if hasattr(img, attr):
|
104
|
+
b64_data = getattr(img, attr)
|
105
|
+
if b64_data:
|
106
|
+
break
|
107
|
+
|
108
|
+
if not b64_data:
|
109
|
+
# If no direct attribute found, try accessing as dictionary
|
110
|
+
try:
|
111
|
+
if hasattr(img, "__dict__"):
|
112
|
+
img_dict = img.__dict__
|
113
|
+
for key in ["b64_json", "image", "base64", "data"]:
|
114
|
+
if key in img_dict and img_dict[key]:
|
115
|
+
b64_data = img_dict[key]
|
116
|
+
break
|
117
|
+
except:
|
118
|
+
pass
|
119
|
+
|
120
|
+
if not b64_data:
|
121
|
+
raise ProcessingError(
|
122
|
+
f"No image data found in API response. Response structure: {dir(img)}"
|
123
|
+
)
|
124
|
+
|
125
|
+
generated_images.append(
|
126
|
+
GeneratedImage(
|
127
|
+
b64_json=b64_data,
|
128
|
+
seed=getattr(img, "seed", None),
|
129
|
+
finish_reason=getattr(img, "finish_reason", None),
|
130
|
+
url=getattr(img, "url", None),
|
131
|
+
)
|
132
|
+
)
|
133
|
+
|
134
|
+
return TogetherAIImageOutput(
|
135
|
+
images=generated_images,
|
136
|
+
model=input_data.model,
|
137
|
+
prompt=input_data.prompt,
|
138
|
+
total_time=total_time,
|
139
|
+
usage=getattr(response, "usage", {}),
|
140
|
+
)
|
141
|
+
|
142
|
+
except Exception as e:
|
143
|
+
raise ProcessingError(f"Together AI image generation failed: {str(e)}")
|
144
|
+
|
145
|
+
def save_images(
|
146
|
+
self, output: TogetherAIImageOutput, output_dir: Path
|
147
|
+
) -> List[Path]:
|
148
|
+
"""
|
149
|
+
Save generated images to disk
|
150
|
+
|
151
|
+
Args:
|
152
|
+
output (TogetherAIImageOutput): Generation output containing images
|
153
|
+
output_dir (Path): Directory to save images
|
154
|
+
|
155
|
+
Returns:
|
156
|
+
List[Path]: List of paths to saved images
|
157
|
+
"""
|
158
|
+
output_dir = Path(output_dir)
|
159
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
160
|
+
|
161
|
+
saved_paths = []
|
162
|
+
for i, img in enumerate(output.images):
|
163
|
+
output_path = output_dir / f"image_{i}.png"
|
164
|
+
image_data = base64.b64decode(img.b64_json)
|
165
|
+
|
166
|
+
with open(output_path, "wb") as f:
|
167
|
+
f.write(image_data)
|
168
|
+
|
169
|
+
saved_paths.append(output_path)
|
170
|
+
|
171
|
+
return saved_paths
|
@@ -0,0 +1,56 @@
|
|
1
|
+
from typing import List, Optional
|
2
|
+
from pydantic import BaseModel, Field, validator
|
3
|
+
from pathlib import Path
|
4
|
+
|
5
|
+
|
6
|
+
class TogetherAIImageInput(BaseModel):
|
7
|
+
"""Schema for Together AI image generation input"""
|
8
|
+
|
9
|
+
prompt: str = Field(..., description="Text prompt for image generation")
|
10
|
+
model: str = Field(
|
11
|
+
default="black-forest-labs/FLUX.1-schnell-Free",
|
12
|
+
description="Together AI image model to use",
|
13
|
+
)
|
14
|
+
steps: int = Field(default=10, description="Number of inference steps", ge=1, le=50)
|
15
|
+
n: int = Field(default=1, description="Number of images to generate", ge=1, le=4)
|
16
|
+
size: str = Field(
|
17
|
+
default="1024x1024", description="Image size in format WIDTHxHEIGHT"
|
18
|
+
)
|
19
|
+
negative_prompt: Optional[str] = Field(
|
20
|
+
default=None, description="Things to exclude from the generation"
|
21
|
+
)
|
22
|
+
seed: Optional[int] = Field(
|
23
|
+
default=None, description="Random seed for reproducibility"
|
24
|
+
)
|
25
|
+
|
26
|
+
@validator("size")
|
27
|
+
def validate_size(cls, v):
|
28
|
+
try:
|
29
|
+
width, height = map(int, v.split("x"))
|
30
|
+
if width <= 0 or height <= 0:
|
31
|
+
raise ValueError
|
32
|
+
return v
|
33
|
+
except:
|
34
|
+
raise ValueError("Size must be in format WIDTHxHEIGHT (e.g., 1024x1024)")
|
35
|
+
|
36
|
+
|
37
|
+
class GeneratedImage(BaseModel):
|
38
|
+
"""Individual generated image data"""
|
39
|
+
|
40
|
+
b64_json: str = Field(..., description="Base64 encoded image data")
|
41
|
+
seed: Optional[int] = Field(None, description="Seed used for this image")
|
42
|
+
finish_reason: Optional[str] = Field(
|
43
|
+
None, description="Reason for finishing generation"
|
44
|
+
)
|
45
|
+
|
46
|
+
|
47
|
+
class TogetherAIImageOutput(BaseModel):
|
48
|
+
"""Schema for Together AI image generation output"""
|
49
|
+
|
50
|
+
images: List[GeneratedImage] = Field(..., description="List of generated images")
|
51
|
+
model: str = Field(..., description="Model used for generation")
|
52
|
+
prompt: str = Field(..., description="Original prompt used")
|
53
|
+
total_time: float = Field(..., description="Time taken for generation in seconds")
|
54
|
+
usage: dict = Field(
|
55
|
+
default_factory=dict, description="Usage statistics and billing information"
|
56
|
+
)
|
@@ -0,0 +1,277 @@
|
|
1
|
+
from typing import Dict, NamedTuple
|
2
|
+
|
3
|
+
|
4
|
+
class ModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
context_length: int
|
8
|
+
quantization: str
|
9
|
+
|
10
|
+
|
11
|
+
TOGETHER_MODELS: Dict[str, ModelConfig] = {
|
12
|
+
# DeepSeek Models
|
13
|
+
"deepseek-ai/DeepSeek-R1": ModelConfig(
|
14
|
+
organization="DeepSeek",
|
15
|
+
display_name="DeepSeek-R1",
|
16
|
+
context_length=32768,
|
17
|
+
quantization="FP8",
|
18
|
+
),
|
19
|
+
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B": ModelConfig(
|
20
|
+
organization="DeepSeek",
|
21
|
+
display_name="DeepSeek R1 Distill Llama 70B",
|
22
|
+
context_length=131072,
|
23
|
+
quantization="FP16",
|
24
|
+
),
|
25
|
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": ModelConfig(
|
26
|
+
organization="DeepSeek",
|
27
|
+
display_name="DeepSeek R1 Distill Qwen 1.5B",
|
28
|
+
context_length=131072,
|
29
|
+
quantization="FP16",
|
30
|
+
),
|
31
|
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-14B": ModelConfig(
|
32
|
+
organization="DeepSeek",
|
33
|
+
display_name="DeepSeek R1 Distill Qwen 14B",
|
34
|
+
context_length=131072,
|
35
|
+
quantization="FP16",
|
36
|
+
),
|
37
|
+
"deepseek-ai/DeepSeek-V3": ModelConfig(
|
38
|
+
organization="DeepSeek",
|
39
|
+
display_name="DeepSeek-V3",
|
40
|
+
context_length=16384,
|
41
|
+
quantization="FP8",
|
42
|
+
),
|
43
|
+
# Meta Models
|
44
|
+
"meta-llama/Llama-3.3-70B-Instruct-Turbo": ModelConfig(
|
45
|
+
organization="Meta",
|
46
|
+
display_name="Llama 3.3 70B Instruct Turbo",
|
47
|
+
context_length=131072,
|
48
|
+
quantization="FP8",
|
49
|
+
),
|
50
|
+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": ModelConfig(
|
51
|
+
organization="Meta",
|
52
|
+
display_name="Llama 3.1 8B Instruct Turbo",
|
53
|
+
context_length=131072,
|
54
|
+
quantization="FP8",
|
55
|
+
),
|
56
|
+
"meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo": ModelConfig(
|
57
|
+
organization="Meta",
|
58
|
+
display_name="Llama 3.1 70B Instruct Turbo",
|
59
|
+
context_length=131072,
|
60
|
+
quantization="FP8",
|
61
|
+
),
|
62
|
+
"meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": ModelConfig(
|
63
|
+
organization="Meta",
|
64
|
+
display_name="Llama 3.1 405B Instruct Turbo",
|
65
|
+
context_length=130815,
|
66
|
+
quantization="FP8",
|
67
|
+
),
|
68
|
+
"meta-llama/Meta-Llama-3-8B-Instruct-Turbo": ModelConfig(
|
69
|
+
organization="Meta",
|
70
|
+
display_name="Llama 3 8B Instruct Turbo",
|
71
|
+
context_length=8192,
|
72
|
+
quantization="FP8",
|
73
|
+
),
|
74
|
+
"meta-llama/Meta-Llama-3-70B-Instruct-Turbo": ModelConfig(
|
75
|
+
organization="Meta",
|
76
|
+
display_name="Llama 3 70B Instruct Turbo",
|
77
|
+
context_length=8192,
|
78
|
+
quantization="FP8",
|
79
|
+
),
|
80
|
+
"meta-llama/Llama-3.2-3B-Instruct-Turbo": ModelConfig(
|
81
|
+
organization="Meta",
|
82
|
+
display_name="Llama 3.2 3B Instruct Turbo",
|
83
|
+
context_length=131072,
|
84
|
+
quantization="FP16",
|
85
|
+
),
|
86
|
+
"meta-llama/Meta-Llama-3-8B-Instruct-Lite": ModelConfig(
|
87
|
+
organization="Meta",
|
88
|
+
display_name="Llama 3 8B Instruct Lite",
|
89
|
+
context_length=8192,
|
90
|
+
quantization="INT4",
|
91
|
+
),
|
92
|
+
"meta-llama/Meta-Llama-3-70B-Instruct-Lite": ModelConfig(
|
93
|
+
organization="Meta",
|
94
|
+
display_name="Llama 3 70B Instruct Lite",
|
95
|
+
context_length=8192,
|
96
|
+
quantization="INT4",
|
97
|
+
),
|
98
|
+
"meta-llama/Llama-3-8b-chat-hf": ModelConfig(
|
99
|
+
organization="Meta",
|
100
|
+
display_name="Llama 3 8B Instruct Reference",
|
101
|
+
context_length=8192,
|
102
|
+
quantization="FP16",
|
103
|
+
),
|
104
|
+
"meta-llama/Llama-3-70b-chat-hf": ModelConfig(
|
105
|
+
organization="Meta",
|
106
|
+
display_name="Llama 3 70B Instruct Reference",
|
107
|
+
context_length=8192,
|
108
|
+
quantization="FP16",
|
109
|
+
),
|
110
|
+
"meta-llama/Llama-2-13b-chat-hf": ModelConfig(
|
111
|
+
organization="Meta",
|
112
|
+
display_name="LLaMA-2 Chat (13B)",
|
113
|
+
context_length=4096,
|
114
|
+
quantization="FP16",
|
115
|
+
),
|
116
|
+
# Nvidia Models
|
117
|
+
"nvidia/Llama-3.1-Nemotron-70B-Instruct-HF": ModelConfig(
|
118
|
+
organization="Nvidia",
|
119
|
+
display_name="Llama 3.1 Nemotron 70B",
|
120
|
+
context_length=32768,
|
121
|
+
quantization="FP16",
|
122
|
+
),
|
123
|
+
# Qwen Models
|
124
|
+
"Qwen/Qwen2.5-Coder-32B-Instruct": ModelConfig(
|
125
|
+
organization="Qwen",
|
126
|
+
display_name="Qwen 2.5 Coder 32B Instruct",
|
127
|
+
context_length=32768,
|
128
|
+
quantization="FP16",
|
129
|
+
),
|
130
|
+
"Qwen/QwQ-32B-Preview": ModelConfig(
|
131
|
+
organization="Qwen",
|
132
|
+
display_name="QwQ-32B-Preview",
|
133
|
+
context_length=32768,
|
134
|
+
quantization="FP16",
|
135
|
+
),
|
136
|
+
"Qwen/Qwen2.5-7B-Instruct-Turbo": ModelConfig(
|
137
|
+
organization="Qwen",
|
138
|
+
display_name="Qwen 2.5 7B Instruct Turbo",
|
139
|
+
context_length=32768,
|
140
|
+
quantization="FP8",
|
141
|
+
),
|
142
|
+
"Qwen/Qwen2.5-72B-Instruct-Turbo": ModelConfig(
|
143
|
+
organization="Qwen",
|
144
|
+
display_name="Qwen 2.5 72B Instruct Turbo",
|
145
|
+
context_length=32768,
|
146
|
+
quantization="FP8",
|
147
|
+
),
|
148
|
+
"Qwen/Qwen2-72B-Instruct": ModelConfig(
|
149
|
+
organization="Qwen",
|
150
|
+
display_name="Qwen 2 Instruct (72B)",
|
151
|
+
context_length=32768,
|
152
|
+
quantization="FP16",
|
153
|
+
),
|
154
|
+
"Qwen/Qwen2-VL-72B-Instruct": ModelConfig(
|
155
|
+
organization="Qwen",
|
156
|
+
display_name="Qwen2 VL 72B Instruct",
|
157
|
+
context_length=32768,
|
158
|
+
quantization="FP16",
|
159
|
+
),
|
160
|
+
# Microsoft Models
|
161
|
+
"microsoft/WizardLM-2-8x22B": ModelConfig(
|
162
|
+
organization="Microsoft",
|
163
|
+
display_name="WizardLM-2 8x22B",
|
164
|
+
context_length=65536,
|
165
|
+
quantization="FP16",
|
166
|
+
),
|
167
|
+
# Google Models
|
168
|
+
"google/gemma-2-27b-it": ModelConfig(
|
169
|
+
organization="Google",
|
170
|
+
display_name="Gemma 2 27B",
|
171
|
+
context_length=8192,
|
172
|
+
quantization="FP16",
|
173
|
+
),
|
174
|
+
"google/gemma-2-9b-it": ModelConfig(
|
175
|
+
organization="Google",
|
176
|
+
display_name="Gemma 2 9B",
|
177
|
+
context_length=8192,
|
178
|
+
quantization="FP16",
|
179
|
+
),
|
180
|
+
"google/gemma-2b-it": ModelConfig(
|
181
|
+
organization="Google",
|
182
|
+
display_name="Gemma Instruct (2B)",
|
183
|
+
context_length=8192,
|
184
|
+
quantization="FP16",
|
185
|
+
),
|
186
|
+
# Databricks Models
|
187
|
+
"databricks/dbrx-instruct": ModelConfig(
|
188
|
+
organization="databricks",
|
189
|
+
display_name="DBRX Instruct",
|
190
|
+
context_length=32768,
|
191
|
+
quantization="FP16",
|
192
|
+
),
|
193
|
+
# Gryphe Models
|
194
|
+
"Gryphe/MythoMax-L2-13b": ModelConfig(
|
195
|
+
organization="Gryphe",
|
196
|
+
display_name="MythoMax-L2 (13B)",
|
197
|
+
context_length=4096,
|
198
|
+
quantization="FP16",
|
199
|
+
),
|
200
|
+
# Mistral AI Models
|
201
|
+
"mistralai/Mistral-Small-24B-Instruct-2501": ModelConfig(
|
202
|
+
organization="mistralai",
|
203
|
+
display_name="Mistral Small 3 Instruct (24B)",
|
204
|
+
context_length=32768,
|
205
|
+
quantization="FP16",
|
206
|
+
),
|
207
|
+
"mistralai/Mistral-7B-Instruct-v0.1": ModelConfig(
|
208
|
+
organization="mistralai",
|
209
|
+
display_name="Mistral (7B) Instruct",
|
210
|
+
context_length=8192,
|
211
|
+
quantization="FP16",
|
212
|
+
),
|
213
|
+
"mistralai/Mistral-7B-Instruct-v0.2": ModelConfig(
|
214
|
+
organization="mistralai",
|
215
|
+
display_name="Mistral (7B) Instruct v0.2",
|
216
|
+
context_length=32768,
|
217
|
+
quantization="FP16",
|
218
|
+
),
|
219
|
+
"mistralai/Mistral-7B-Instruct-v0.3": ModelConfig(
|
220
|
+
organization="mistralai",
|
221
|
+
display_name="Mistral (7B) Instruct v0.3",
|
222
|
+
context_length=32768,
|
223
|
+
quantization="FP16",
|
224
|
+
),
|
225
|
+
"mistralai/Mixtral-8x7B-Instruct-v0.1": ModelConfig(
|
226
|
+
organization="mistralai",
|
227
|
+
display_name="Mixtral-8x7B Instruct (46.7B)",
|
228
|
+
context_length=32768,
|
229
|
+
quantization="FP16",
|
230
|
+
),
|
231
|
+
"mistralai/Mixtral-8x22B-Instruct-v0.1": ModelConfig(
|
232
|
+
organization="mistralai",
|
233
|
+
display_name="Mixtral-8x22B Instruct (141B)",
|
234
|
+
context_length=65536,
|
235
|
+
quantization="FP16",
|
236
|
+
),
|
237
|
+
# NousResearch Models
|
238
|
+
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": ModelConfig(
|
239
|
+
organization="NousResearch",
|
240
|
+
display_name="Nous Hermes 2 - Mixtral 8x7B-DPO (46.7B)",
|
241
|
+
context_length=32768,
|
242
|
+
quantization="FP16",
|
243
|
+
),
|
244
|
+
# Upstage Models
|
245
|
+
"upstage/SOLAR-10.7B-Instruct-v1.0": ModelConfig(
|
246
|
+
organization="upstage",
|
247
|
+
display_name="Upstage SOLAR Instruct v1 (11B)",
|
248
|
+
context_length=4096,
|
249
|
+
quantization="FP16",
|
250
|
+
),
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
def get_model_config(model_id: str) -> ModelConfig:
|
255
|
+
"""Get model configuration by model ID"""
|
256
|
+
if model_id not in TOGETHER_MODELS:
|
257
|
+
raise ValueError(f"Model {model_id} not found in Together AI models")
|
258
|
+
return TOGETHER_MODELS[model_id]
|
259
|
+
|
260
|
+
|
261
|
+
def list_models_by_organization(organization: str) -> Dict[str, ModelConfig]:
|
262
|
+
"""Get all models for a specific organization"""
|
263
|
+
return {
|
264
|
+
model_id: config
|
265
|
+
for model_id, config in TOGETHER_MODELS.items()
|
266
|
+
if config.organization.lower() == organization.lower()
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
def get_default_model() -> str:
|
271
|
+
"""Get the default model ID"""
|
272
|
+
return "meta-llama/Llama-3.3-70B-Instruct-Turbo"
|
273
|
+
|
274
|
+
|
275
|
+
if __name__ == "__main__":
|
276
|
+
print(len(TOGETHER_MODELS))
|
277
|
+
print(get_model_config("meta-llama/Llama-3.3-70B-Instruct-Turbo"))
|
@@ -0,0 +1,43 @@
|
|
1
|
+
from typing import Dict, NamedTuple
|
2
|
+
|
3
|
+
|
4
|
+
class RerankModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
model_size: str
|
8
|
+
max_doc_size: int
|
9
|
+
max_docs: int
|
10
|
+
|
11
|
+
|
12
|
+
TOGETHER_RERANK_MODELS: Dict[str, RerankModelConfig] = {
|
13
|
+
"Salesforce/Llama-Rank-v1": RerankModelConfig(
|
14
|
+
organization="Salesforce",
|
15
|
+
display_name="LlamaRank",
|
16
|
+
model_size="8B",
|
17
|
+
max_doc_size=8192,
|
18
|
+
max_docs=1024,
|
19
|
+
)
|
20
|
+
}
|
21
|
+
|
22
|
+
|
23
|
+
def get_rerank_model_config(model_id: str) -> RerankModelConfig:
|
24
|
+
"""Get rerank model configuration by model ID"""
|
25
|
+
if model_id not in TOGETHER_RERANK_MODELS:
|
26
|
+
raise ValueError(f"Model {model_id} not found in Together AI rerank models")
|
27
|
+
return TOGETHER_RERANK_MODELS[model_id]
|
28
|
+
|
29
|
+
|
30
|
+
def list_rerank_models_by_organization(
|
31
|
+
organization: str,
|
32
|
+
) -> Dict[str, RerankModelConfig]:
|
33
|
+
"""Get all rerank models for a specific organization"""
|
34
|
+
return {
|
35
|
+
model_id: config
|
36
|
+
for model_id, config in TOGETHER_RERANK_MODELS.items()
|
37
|
+
if config.organization.lower() == organization.lower()
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
def get_default_rerank_model() -> str:
|
42
|
+
"""Get the default rerank model ID"""
|
43
|
+
return "Salesforce/Llama-Rank-v1"
|
@@ -0,0 +1,49 @@
|
|
1
|
+
from typing import Optional
|
2
|
+
from together import Together
|
3
|
+
from airtrain.core.skills import Skill, ProcessingError
|
4
|
+
from .credentials import TogetherAICredentials
|
5
|
+
from .schemas import TogetherAIRerankInput, TogetherAIRerankOutput, RerankResult
|
6
|
+
from .rerank_models_config import get_rerank_model_config
|
7
|
+
|
8
|
+
|
9
|
+
class TogetherAIRerankSkill(Skill[TogetherAIRerankInput, TogetherAIRerankOutput]):
|
10
|
+
"""Skill for reranking documents using Together AI"""
|
11
|
+
|
12
|
+
input_schema = TogetherAIRerankInput
|
13
|
+
output_schema = TogetherAIRerankOutput
|
14
|
+
|
15
|
+
def __init__(self, credentials: Optional[TogetherAICredentials] = None):
|
16
|
+
"""Initialize the skill with optional credentials"""
|
17
|
+
super().__init__()
|
18
|
+
self.credentials = credentials or TogetherAICredentials.from_env()
|
19
|
+
self.client = Together(
|
20
|
+
api_key=self.credentials.together_api_key.get_secret_value()
|
21
|
+
)
|
22
|
+
|
23
|
+
def process(self, input_data: TogetherAIRerankInput) -> TogetherAIRerankOutput:
|
24
|
+
try:
|
25
|
+
# Validate the model exists in our config
|
26
|
+
get_rerank_model_config(input_data.model)
|
27
|
+
|
28
|
+
# Call Together AI rerank API
|
29
|
+
response = self.client.rerank.create(
|
30
|
+
model=input_data.model,
|
31
|
+
query=input_data.query,
|
32
|
+
documents=input_data.documents,
|
33
|
+
top_n=input_data.top_n,
|
34
|
+
)
|
35
|
+
|
36
|
+
# Transform results
|
37
|
+
results = [
|
38
|
+
RerankResult(
|
39
|
+
index=result.index,
|
40
|
+
relevance_score=result.relevance_score,
|
41
|
+
document=input_data.documents[result.index],
|
42
|
+
)
|
43
|
+
for result in response.results
|
44
|
+
]
|
45
|
+
|
46
|
+
return TogetherAIRerankOutput(results=results, used_model=input_data.model)
|
47
|
+
|
48
|
+
except Exception as e:
|
49
|
+
raise ProcessingError(f"Together AI reranking failed: {str(e)}")
|
@@ -0,0 +1,33 @@
|
|
1
|
+
from typing import List, Optional
|
2
|
+
from pydantic import Field, BaseModel
|
3
|
+
from airtrain.core.schemas import InputSchema, OutputSchema
|
4
|
+
|
5
|
+
|
6
|
+
class RerankResult(BaseModel):
|
7
|
+
"""Schema for individual rerank result"""
|
8
|
+
|
9
|
+
index: int = Field(..., description="Index of the document in original list")
|
10
|
+
relevance_score: float = Field(..., description="Relevance score for the document")
|
11
|
+
document: str = Field(..., description="The document content")
|
12
|
+
|
13
|
+
|
14
|
+
class TogetherAIRerankInput(InputSchema):
|
15
|
+
"""Schema for Together AI rerank input"""
|
16
|
+
|
17
|
+
query: str = Field(..., description="Query to rank documents against")
|
18
|
+
documents: List[str] = Field(..., description="List of documents to rank")
|
19
|
+
model: str = Field(
|
20
|
+
default="Salesforce/Llama-Rank-v1",
|
21
|
+
description="Together AI rerank model to use",
|
22
|
+
)
|
23
|
+
top_n: Optional[int] = Field(
|
24
|
+
default=None,
|
25
|
+
description="Number of top results to return. If None, returns all results",
|
26
|
+
)
|
27
|
+
|
28
|
+
|
29
|
+
class TogetherAIRerankOutput(OutputSchema):
|
30
|
+
"""Schema for Together AI rerank output"""
|
31
|
+
|
32
|
+
results: List[RerankResult] = Field(..., description="Ranked results")
|
33
|
+
used_model: str = Field(..., description="Model used for ranking")
|
@@ -1,8 +1,13 @@
|
|
1
|
-
from typing import Optional, Dict, Any
|
1
|
+
from typing import Optional, Dict, Any, List
|
2
2
|
from pydantic import Field
|
3
3
|
from airtrain.core.skills import Skill, ProcessingError
|
4
4
|
from airtrain.core.schemas import InputSchema, OutputSchema
|
5
5
|
from .credentials import TogetherAICredentials
|
6
|
+
from .models import TogetherAIImageInput, TogetherAIImageOutput, GeneratedImage
|
7
|
+
from pathlib import Path
|
8
|
+
import base64
|
9
|
+
import time
|
10
|
+
from together import Together
|
6
11
|
|
7
12
|
|
8
13
|
class TogetherAIInput(InputSchema):
|
@@ -41,3 +46,84 @@ class TogetherAIChatSkill(Skill[TogetherAIInput, TogetherAIOutput]):
|
|
41
46
|
|
42
47
|
def process(self, input_data: TogetherAIInput) -> TogetherAIOutput:
|
43
48
|
raise NotImplementedError("TogetherAIChatSkill is not implemented yet")
|
49
|
+
|
50
|
+
|
51
|
+
class TogetherAIImageSkill(Skill[TogetherAIImageInput, TogetherAIImageOutput]):
|
52
|
+
"""Skill for Together AI image generation"""
|
53
|
+
|
54
|
+
input_schema = TogetherAIImageInput
|
55
|
+
output_schema = TogetherAIImageOutput
|
56
|
+
|
57
|
+
def __init__(self, credentials: Optional[TogetherAICredentials] = None):
|
58
|
+
super().__init__()
|
59
|
+
self.credentials = credentials or TogetherAICredentials.from_env()
|
60
|
+
self.client = Together(
|
61
|
+
api_key=self.credentials.together_api_key.get_secret_value()
|
62
|
+
)
|
63
|
+
|
64
|
+
def process(self, input_data: TogetherAIImageInput) -> TogetherAIImageOutput:
|
65
|
+
try:
|
66
|
+
start_time = time.time()
|
67
|
+
|
68
|
+
# Generate images
|
69
|
+
response = self.client.images.generate(
|
70
|
+
prompt=input_data.prompt,
|
71
|
+
model=input_data.model,
|
72
|
+
steps=input_data.steps,
|
73
|
+
n=input_data.n,
|
74
|
+
size=input_data.size,
|
75
|
+
negative_prompt=input_data.negative_prompt,
|
76
|
+
seed=input_data.seed,
|
77
|
+
)
|
78
|
+
|
79
|
+
# Calculate total time
|
80
|
+
total_time = time.time() - start_time
|
81
|
+
|
82
|
+
# Convert response to our output format
|
83
|
+
generated_images = [
|
84
|
+
GeneratedImage(
|
85
|
+
b64_json=img.b64_json,
|
86
|
+
seed=getattr(img, "seed", None),
|
87
|
+
finish_reason=getattr(img, "finish_reason", None),
|
88
|
+
)
|
89
|
+
for img in response.data
|
90
|
+
]
|
91
|
+
|
92
|
+
return TogetherAIImageOutput(
|
93
|
+
images=generated_images,
|
94
|
+
model=input_data.model,
|
95
|
+
prompt=input_data.prompt,
|
96
|
+
total_time=total_time,
|
97
|
+
usage=getattr(response, "usage", {}),
|
98
|
+
)
|
99
|
+
|
100
|
+
except Exception as e:
|
101
|
+
raise ProcessingError(f"Together AI image generation failed: {str(e)}")
|
102
|
+
|
103
|
+
def save_images(
|
104
|
+
self, output: TogetherAIImageOutput, output_dir: Path
|
105
|
+
) -> List[Path]:
|
106
|
+
"""
|
107
|
+
Save generated images to disk
|
108
|
+
|
109
|
+
Args:
|
110
|
+
output (TogetherAIImageOutput): Generation output containing images
|
111
|
+
output_dir (Path): Directory to save images
|
112
|
+
|
113
|
+
Returns:
|
114
|
+
List[Path]: List of paths to saved images
|
115
|
+
"""
|
116
|
+
output_dir = Path(output_dir)
|
117
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
118
|
+
|
119
|
+
saved_paths = []
|
120
|
+
for i, img in enumerate(output.images):
|
121
|
+
output_path = output_dir / f"image_{i}.png"
|
122
|
+
image_data = base64.b64decode(img.b64_json)
|
123
|
+
|
124
|
+
with open(output_path, "wb") as f:
|
125
|
+
f.write(image_data)
|
126
|
+
|
127
|
+
saved_paths.append(output_path)
|
128
|
+
|
129
|
+
return saved_paths
|
@@ -0,0 +1,49 @@
|
|
1
|
+
from typing import Dict, NamedTuple
|
2
|
+
|
3
|
+
|
4
|
+
class VisionModelConfig(NamedTuple):
|
5
|
+
organization: str
|
6
|
+
display_name: str
|
7
|
+
context_length: int
|
8
|
+
|
9
|
+
|
10
|
+
TOGETHER_VISION_MODELS: Dict[str, VisionModelConfig] = {
|
11
|
+
"meta-llama/Llama-Vision-Free": VisionModelConfig(
|
12
|
+
organization="Meta",
|
13
|
+
display_name="(Free) Llama 3.2 11B Vision Instruct Turbo",
|
14
|
+
context_length=131072,
|
15
|
+
),
|
16
|
+
"meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo": VisionModelConfig(
|
17
|
+
organization="Meta",
|
18
|
+
display_name="Llama 3.2 11B Vision Instruct Turbo",
|
19
|
+
context_length=131072,
|
20
|
+
),
|
21
|
+
"meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo": VisionModelConfig(
|
22
|
+
organization="Meta",
|
23
|
+
display_name="Llama 3.2 90B Vision Instruct Turbo",
|
24
|
+
context_length=131072,
|
25
|
+
),
|
26
|
+
}
|
27
|
+
|
28
|
+
|
29
|
+
def get_vision_model_config(model_id: str) -> VisionModelConfig:
|
30
|
+
"""Get vision model configuration by model ID"""
|
31
|
+
if model_id not in TOGETHER_VISION_MODELS:
|
32
|
+
raise ValueError(f"Model {model_id} not found in Together AI vision models")
|
33
|
+
return TOGETHER_VISION_MODELS[model_id]
|
34
|
+
|
35
|
+
|
36
|
+
def list_vision_models_by_organization(
|
37
|
+
organization: str,
|
38
|
+
) -> Dict[str, VisionModelConfig]:
|
39
|
+
"""Get all vision models for a specific organization"""
|
40
|
+
return {
|
41
|
+
model_id: config
|
42
|
+
for model_id, config in TOGETHER_VISION_MODELS.items()
|
43
|
+
if config.organization.lower() == organization.lower()
|
44
|
+
}
|
45
|
+
|
46
|
+
|
47
|
+
def get_default_vision_model() -> str:
|
48
|
+
"""Get the default vision model ID"""
|
49
|
+
return "meta-llama/Llama-Vision-Free"
|
@@ -1,6 +1,6 @@
|
|
1
|
-
airtrain/__init__.py,sha256=
|
1
|
+
airtrain/__init__.py,sha256=BwgzBp8NKID9GrpW2RUmCyV6Aj_wlKI9HJqHTe6blMw,2095
|
2
2
|
airtrain/contrib/__init__.py,sha256=pG-7mJ0pBMqp3Q86mIF9bo1PqoBOVSGlnEK1yY1U1ok,641
|
3
|
-
airtrain/contrib/travel/__init__.py,sha256=
|
3
|
+
airtrain/contrib/travel/__init__.py,sha256=clmBodw4nkTA-DsgjVGcXfJGPaWxIpCZDtdO-8RzL0M,811
|
4
4
|
airtrain/contrib/travel/agents.py,sha256=tpQtZ0WUiXBuhvZtc2JlEam5TuR5l-Tndi14YyImDBM,8975
|
5
5
|
airtrain/contrib/travel/models.py,sha256=E4Mtds5TINmXLXu65aTYqv6wwOh2KclJHZ2eXRrBqiY,1547
|
6
6
|
airtrain/core/__init__.py,sha256=9h7iKwTzZocCPc9bU6j8bA02BokteWIOcO1uaqGMcrk,254
|
@@ -29,14 +29,25 @@ airtrain/integrations/ollama/skills.py,sha256=M_Un8D5VJ5XtPEq9IClzqV3jCPBoFTSm2v
|
|
29
29
|
airtrain/integrations/openai/__init__.py,sha256=K-NY2_T1T6SEOgkpbUA55cWvK2nr2NOJgLCqmmtaCno,371
|
30
30
|
airtrain/integrations/openai/chinese_assistant.py,sha256=MMhv4NBOoEQ0O22ZZtP255rd5ajHC9l6FPWIjpqxBOA,1581
|
31
31
|
airtrain/integrations/openai/credentials.py,sha256=NfRyp1QgEtgm8cxt2-BOLq-6d0X-Pcm80NnfHM8p0FY,1470
|
32
|
+
airtrain/integrations/openai/models_config.py,sha256=bzosqqpDy2AJxu2vGdk2H4voqEGlv7LORR6fpJLhNic,3962
|
32
33
|
airtrain/integrations/openai/skills.py,sha256=Olg9-6f_p2XgkVwwcB9tvjAMApmM2EK81i8LP4qVVvs,7676
|
33
34
|
airtrain/integrations/sambanova/__init__.py,sha256=dp_263iOckM_J9pOEvyqpf3FrejD6-_x33r0edMCTe0,179
|
34
35
|
airtrain/integrations/sambanova/credentials.py,sha256=U36RAEIPNuwo-vTrt3U9kkkj2GfdqSclA1ttOYHxS-w,784
|
35
36
|
airtrain/integrations/sambanova/skills.py,sha256=Po1ur_QFwzVIugbkk2mt73WdXDz_Gr9ASlUc9Y12Kok,1614
|
36
37
|
airtrain/integrations/together/__init__.py,sha256=we4KXn_pUs6Dxo3QcB-t40BSRraQFdKg2nXw7yi2FjM,185
|
37
|
-
airtrain/integrations/together/
|
38
|
-
airtrain/integrations/together/
|
39
|
-
airtrain
|
40
|
-
airtrain
|
41
|
-
airtrain
|
42
|
-
airtrain
|
38
|
+
airtrain/integrations/together/audio_models_config.py,sha256=GtqfmKR1vJ5x4B3kScvEO3x4exvzwNP78vcGVTk_fBE,1004
|
39
|
+
airtrain/integrations/together/credentials.py,sha256=cYNhyIwgsxm8LfiFfT-omBvgV3mUP6SZeRSukyzzDlI,747
|
40
|
+
airtrain/integrations/together/embedding_models_config.py,sha256=F0ISAXCG_Pcnf-ojkvZwIXacXD8LaU8hQmGHCFzmlds,2927
|
41
|
+
airtrain/integrations/together/image_models_config.py,sha256=JlCozrphI9zE4uYpGfj4DCWSN6GZGyr84Tb1HmjNQ28,2455
|
42
|
+
airtrain/integrations/together/image_skill.py,sha256=FMO9-TRwfucLRlpvij9VpeVsrTiP9wux_je_pFz6OXs,6508
|
43
|
+
airtrain/integrations/together/models.py,sha256=ZW5xfEN9fU18aeltb-sB2O-Bnu5sLkDPZqvUtxgoH-U,2112
|
44
|
+
airtrain/integrations/together/models_config.py,sha256=XMKp0Oq1nWWnMMdNAZxkFXmJaURwWrwLE18kFXsMsRw,8829
|
45
|
+
airtrain/integrations/together/rerank_models_config.py,sha256=coCg0IOG2tU4L2uc2uPtPdoBwGjSc_zQwxENwdDuwHE,1188
|
46
|
+
airtrain/integrations/together/rerank_skill.py,sha256=gjH24hLWCweWKPyyfKZMG3K_g9gWzm80WgiJNjkA9eg,1894
|
47
|
+
airtrain/integrations/together/schemas.py,sha256=pBMrbX67oxPCr-sg4K8_Xqu1DWbaC4uLCloVSascROg,1210
|
48
|
+
airtrain/integrations/together/skills.py,sha256=UfLHnseZbA7R7q5dDco6mpV546Zfd3DTliZSrNkCL6Q,4518
|
49
|
+
airtrain/integrations/together/vision_models_config.py,sha256=m28HwYDk2Kup_J-a1FtynIa2ZVcbl37kltfoHnK8zxs,1544
|
50
|
+
airtrain-0.1.13.dist-info/METADATA,sha256=kvTff3-jQ_th6KyfKrSQ8EOjjq5SiyHwq802VFYKqCg,4536
|
51
|
+
airtrain-0.1.13.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
52
|
+
airtrain-0.1.13.dist-info/top_level.txt,sha256=cFWW1vY6VMCb3AGVdz6jBDpZ65xxBRSqlsPyySxTkxY,9
|
53
|
+
airtrain-0.1.13.dist-info/RECORD,,
|
File without changes
|
File without changes
|