File size: 3,181 Bytes
9c12531
 
 
 
 
c7bc262
9c12531
 
 
 
 
8b66151
 
c7bc262
 
8b66151
ce2af7e
c7bc262
 
 
ce2af7e
 
8b66151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88a0b72
8b66151
 
 
 
 
 
88a0b72
 
 
 
 
8b66151
 
 
 
 
 
 
 
 
 
 
 
 
 
88a0b72
8b66151
 
88a0b72
 
8b66151
88a0b72
8b66151
 
 
88a0b72
 
 
 
 
9c12531
0abdfaa
8b66151
 
 
 
9c12531
 
 
 
 
 
 
 
c7bc262
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
import gradio as gr
from openai import OpenAI
import os

ACCESS_TOKEN = os.getenv("HF_TOKEN")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1/",
    api_key=ACCESS_TOKEN,
)

# Search Tool
from langchain_community.tools.tavily_search import TavilySearchResults
search_tool = TavilySearchResults(tavily_api_key=TAVILY_API_KEY)

# Define a comprehensive system prompt
SYSTEM_PROMPT = """
You are a highly knowledgeable and reliable Crypto Trading Advisor and Analyzer. 
Your goal is to assist users in understanding, analyzing, and making informed decisions about cryptocurrency trading. 
You provide accurate, concise, and actionable advice based on real-time data, historical trends, and established best practices.
"""

# Fixed settings for LLM
MAX_TOKENS = 512
TEMPERATURE = 0.3
TOP_P = 0.95
FREQUENCY_PENALTY = 0.0
SEED = -1  # Use None if random seed is preferred

# Function to handle chatbot responses
def respond(message, history: list[tuple[str, str]]):
    print(f"Received message: {message}")
    print(f"History: {history}")

    # Convert seed to None if -1 (meaning random)
    if SEED == -1:
        seed = None
    else:
        seed = SEED

    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    print("System prompt added to messages.")

    # Add conversation history to the context
    for val in history:
        user_part = val[0]
        assistant_part = val[1]
        if user_part:
            messages.append({"role": "user", "content": user_part})
        if assistant_part:
            messages.append({"role": "assistant", "content": assistant_part})

    # Append the latest user message
    messages.append({"role": "user", "content": message})

    # Use the search tool to get relevant results based on the message
    search_results = search_tool.search(message)
    if search_results:
        # Combine search results into a string to send to the model
        search_results_text = "Here are the search results:\n"
        for result in search_results:
            search_results_text += f"- {result['title']}: {result['url']}\n"
        # Add the search results to the messages to be used by LLM
        messages.append({"role": "assistant", "content": search_results_text})

    # Start response generation
    response = ""
    print("Sending request to OpenAI API.")

    for message_chunk in client.chat.completions.create(
        model="meta-llama/Llama-3.3-70B-Instruct",
        max_tokens=MAX_TOKENS,
        stream=True,
        temperature=TEMPERATURE,
        top_p=TOP_P,
        frequency_penalty=FREQUENCY_PENALTY,
        seed=seed,
        messages=messages,
    ):
        token_text = message_chunk.choices[0].delta.content
        response += token_text
        yield response

    print("Completed response generation.")

# Gradio UI (no sliders for the user, settings are fixed in code)
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Ask about crypto trading or analysis.", likeable=True)

demo = gr.ChatInterface(
    fn=respond,
    fill_height=True,
    chatbot=chatbot,
)

if __name__ == "__main__":
    demo.launch()