File size: 1,861 Bytes
b81ac13
860db05
b81ac13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
860db05
 
 
 
 
 
 
 
b81ac13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
# from llama_index.llms.openllm import OpenLLM
from llama_index.llms.nebius import NebiusLLM

# ...existing environment variable loading logic...
from dotenv import load_dotenv
load_dotenv()

LLM_PROVIDER = os.environ.get("LLM_PROVIDER", "openllm").lower()
LLM_API_URL = os.environ.get("LLM_API_URL")
LLM_API_KEY = os.environ.get("LLM_API_KEY")
NEBIUS_API_KEY = os.environ.get("NEBIUS_API_KEY", "")
OPENLLM_MODEL = os.environ.get("OPENLLM_MODEL")
NEBIUS_MODEL = os.environ.get("NEBIUS_MODEL")

if LLM_PROVIDER == "nebius":
    llm = NebiusLLM(
        api_key=NEBIUS_API_KEY,
        model=NEBIUS_MODEL
    )
else:
    pass
    # llm = OpenLLM(
    #     model=OPENLLM_MODEL,
    #     api_base=LLM_API_URL,
    #     api_key=LLM_API_KEY,
    #     max_new_tokens=2048,
    #     temperature=0.7,
    # )

import re

def call_llm_api(messages):
    """
    Calls the LLM API endpoint with the conversation messages using OpenLLM or NebiusLLM.
    Args:
        messages (list): List of dicts with 'role' and 'content' for each message.
    Returns:
        str: The assistant's reply as a string.
    """
    from llama_index.core.llms import ChatMessage
    chat_messages = [ChatMessage(role=m["role"], content=m["content"]) for m in messages]
    response = llm.chat(chat_messages)
    return response.message.content

def is_stage_complete(llm_reply):
    """
    Heuristic to determine if the current stage is complete based on LLM reply.
    Args:
        llm_reply (str): The assistant's reply.
    Returns:
        bool: True if the stage is considered complete, False otherwise.
    """
    triggers = [
        "stage complete",
        "let's move to the next stage",
        "moving to the next stage",
        "next stage",
        "you have completed this stage"
    ]
    return any(trigger in llm_reply.lower() for trigger in triggers)