File size: 3,703 Bytes
8771845
 
 
 
 
 
 
 
 
 
 
 
 
 
5295930
8771845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
'''
 Fintelligence (C) 2024
 Intelligent Finance
 Released in Apache 2.0 license

'''
import streamlit as st
import os
from openai import OpenAI
import json

# Initialize the client
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1",
    api_key=os.environ.get('HUGGINGFACE_API_TOKEN')
)

# Model configuration
MODEL = "HuggingFaceH4/zephyr-7b-beta"

# Define a custom function
def get_current_weather(location, unit="celsius"):
    """Get the current weather in a given location"""
    weather_info = {
        "location": location,
        "temperature": "22",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return weather_info

# Set up the Streamlit app
st.title("Chatbot with Hugging Face and Zephyr-7B-Beta")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Accept user input
if prompt := st.chat_input("What is up?"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)

    # Prepare the messages for the API call
    messages = [
        {"role": msg["role"], "content": msg["content"]}
        for msg in st.session_state.messages
    ]

    # Call the Hugging Face API
    try:
        response = client.chat.completions.create(
            model=MODEL,
            messages=messages,
            functions=[
                {
                    "name": "get_current_weather",
                    "description": "Get the current weather in a given location",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {"type": "string"},
                            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                        },
                        "required": ["location"],
                    },
                }
            ],
            function_call="auto",
        )

        # Process the response
        assistant_response = response.choices[0].message.content
        function_call = response.choices[0].message.function_call

        if function_call:
            function_name = function_call.name
            function_args = json.loads(function_call.arguments)
            
            if function_name == "get_current_weather":
                function_response = get_current_weather(**function_args)
                
                # Call the API again with the function response
                messages.append({"role": "function", "name": function_name, "content": json.dumps(function_response)})
                final_response = client.chat.completions.create(
                    model=MODEL,
                    messages=messages
                )
                assistant_response = final_response.choices[0].message.content

        # Display assistant response in chat message container
        with st.chat_message("assistant"):
            st.markdown(assistant_response)
        
        # Add assistant response to chat history
        st.session_state.messages.append({"role": "assistant", "content": assistant_response})

    except Exception as e:
        st.error(f"An error occurred: {str(e)}")

    # Note: Token usage information is not available with the Hugging Face API
    st.sidebar.write("Note: Token usage information is not available with the Hugging Face API.")