File size: 2,873 Bytes
48ec86e
 
 
 
3305d00
48ec86e
2027c04
48ec86e
 
2027c04
 
48ec86e
 
 
 
 
 
 
 
2027c04
48ec86e
 
 
2027c04
 
 
 
 
 
dd9518b
3305d00
 
 
 
 
 
 
 
2027c04
dd9518b
 
 
 
2027c04
3305d00
 
 
 
 
48ec86e
 
 
 
 
2027c04
48ec86e
 
 
 
 
 
dd9518b
48ec86e
 
 
3305d00
48ec86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2027c04
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ruff: noqa: F401

import os
import sys
from platform import node

import rich
from loguru import logger
from smolagents import LiteLLMModel, OpenAIServerModel

from exit_gracefully import exit_gracefully

print = rich.get_console().print  # noqa


def openai_model(
    model_id = None,
    api_base = None,
    api_key = None,
    **kwargs,
):
    kwargs = kwargs or {}
    # default siliconflow
    # api_base = api_base or "https://api.siliconflow.cn/v1"
    # api_key = api_key or os.getenv("SILICONFLOW_API_KEY")
    # model_id = model_id or "deepseek-ai/DeepSeek-V3"

    # default llama4
    api_base = api_base or "https://api.llama.com/compat/v1"

    if "golay" in node() and ("llama.com" in api_base or "openai.com" in api_base):
        os.environ.update(
            HTTPS_PROXY="http://localhost:8081",
            HTTP_PROXY="http://localhost:8081",
            ALL_PROXY="http://localhost:8081",
            NO_PROXY="localhost,127.0.0.1",
        )

    api_key = api_key or os.getenv("LLAMA_API_KEY")
    if isinstance(api_key, str):
        # LLAMA_API_KEY contains | and in win10 need to assign env var with ""
        api_key = api_key.strip('"')
    assert api_key, "LLAMA_API_KEY not set, set it and try again"

    default =  "Llama-4-Maverick-17B-128E-Instruct-FP8"
    # default = "Llama-4-Scout-17B-16E-Instruct-FP8"
    logger.debug(f"{default=}")

    model_id = model_id or default

    return OpenAIServerModel(
        model_id,
        api_base=api_base,
        api_key=api_key,
        # temperature=0.,
        **kwargs,
    )

def main():
    messages = [{'role': 'user', 'content': 'Say this is a test.'}]
    logger.debug(sys.argv)

    if not sys.argv[1:]:
        model = openai_model()
        logger.debug(model(messages))
        return

    if len(sys.argv[1:]) < 3:
        raise SystemExit("Provide at least three args (model_id, api_base, api_key)")

    model_id, api_base, api_key, *_ = sys.argv[1:]
    model = openai_model(model_id, api_base, api_key)
    try:
        response = model(messages)
        logger.debug(response)
    except Exception as e:
        logger.error(e)
        return
    try:
        print(response.content)
    except Exception as e:
        logger.error(e)


if __name__ == "__main__":
    main()
    # python openai_model.py
    #  deepseek-ai/DeepSeek-V3 https://api.siliconflow.cn/v1 %SILICONFLOW_API_KEY%

    # python openai_model.py  grok-3-beta https://api.x.ai/v1 %XAI_API_KEY%

    #   gemini-2.5-flash-preview-04-17  https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY%
    #   gemini-2.0-flash

    # https://api.together.ai/models/deepseek-ai/DeepSeek-V3
    # deepseek-ai/DeepSeek-V3 https://api.together.xyz/v1 %TOGETHER_API_KEY%

    #  deepseek-chat  https://litellm.dattw.eu.org/v1 %LITELLM_API_KEY%

    #

    # LLM API proxy: https://linux.do/t/topic/290871