File size: 2,119 Bytes
cbddf51
38b4c24
 
cbddf51
7a6bda4
 
cbddf51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38b4c24
cbddf51
38b4c24
 
cbddf51
38b4c24
cbddf51
38b4c24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbddf51
38b4c24
 
cbddf51
 
 
 
 
 
 
 
7a6bda4
0dd2166
cbddf51
38b4c24
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from smolagents.tools import Tool
from anthropic import Anthropic
import os
import json
from dotenv import load_dotenv
load_dotenv()  # Loads variables from .env into environment

class NeedToDestinationTool(Tool):
    name = "NeedToDestination"
    inputs = {
        "need": {"type": "string", "description": "User's travel need as text"},
    }
    output_type = "array"
    description = "Suggests destinations and flight info based on user need."

    def __init__(self, model: callable, departure_airport: str = "CDG") -> None:
        super().__init__()
        self.model = model
        self.departure_airport = departure_airport

    def forward(self, need: str) -> list[dict]:
        prompt = f"""
            You are a travel agent AI.

            Based on the user's need: "{need}",
            suggest 2-3 travel destinations with round-trip flight information.

            Return the output as valid JSON in the following format:

            [
            {{
                "destination": "DestinationName",
                "departure": {{
                "date": "YYYY-MM-DD",
                "from_airport": "{self.departure_airport}",
                "to_airport": "XXX"
                }},
                "return": {{
                "date": "YYYY-MM-DD",
                "from_airport": "XXX",
                "to_airport": "{self.departure_airport}"
                }}
            }},
            ...
            ]

            DO NOT add explanations, only return raw JSON.
            """
        result = self.model(prompt)
        try:
            destinations = json.loads(result.strip())
        except json.JSONDecodeError:
            raise ValueError("Could not parse LLM output to JSON.")

        return destinations
    

client = Anthropic(api_key=os.getenv("ANTROPIC_KEY"))

def claude_need_to_destination_model(prompt: str) -> str:
    message = client.messages.create(
        model="claude-3-opus-20240229",
        max_tokens=1024,
        temperature=0.7,
        messages=[
            {"role": "user", "content": prompt}
        ]
    )
    return message.content[0].text