davoodwadi commited on
Commit
a5092a7
·
verified ·
1 Parent(s): ae11118

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import streamlit as st
4
+ import os
5
+
6
+ # Define the file path for the configuration file
7
+ # file_path = 'config.json'
8
+
9
+ # Open and read the contents of the JSON file
10
+ # with open(file_path, 'r') as json_file:
11
+ # creds = json.load(json_file)
12
+ FIREWORKS_API_KEY = os.environ['FIREWORKS_API_KEY']
13
+ def get_response(messages):
14
+ """
15
+ Make a POST request and return the response content.
16
+
17
+ :param messages: The list of messages to send in the body of the POST request.
18
+ :return: The response content.
19
+ """
20
+ url = "https://api.fireworks.ai/inference/v1/chat/completions"
21
+ payload = {
22
+ "model": "accounts/fireworks/models/llama-v3p1-8b-instruct",
23
+ "max_tokens": 16384,
24
+ "top_p": 1,
25
+ "top_k": 40,
26
+ "presence_penalty": 0,
27
+ "frequency_penalty": 0,
28
+ "temperature": 0.6,
29
+ "messages": messages
30
+ }
31
+
32
+ headers = {
33
+ "Accept": "application/json",
34
+ "Content-Type": "application/json",
35
+ "Authorization": f"Bearer {FIREWORKS_API_KEY}"
36
+ }
37
+
38
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
39
+ response.raise_for_status() # Raise an error for bad responses
40
+
41
+ response_content = response.json()
42
+ # print(response_content) # Debugging: print the response content
43
+ return response_content['choices'][0]['message']['content']
44
+
45
+ # Initialize chat history
46
+ if "messages" not in st.session_state:
47
+ st.session_state.messages = []
48
+
49
+ # Display chat messages from history
50
+ for message in st.session_state.messages:
51
+ with st.chat_message(message["role"]):
52
+ st.markdown(message["content"])
53
+
54
+ # Accept user input
55
+ if prompt := st.chat_input("What is up?"):
56
+ # Add user message to chat history
57
+ st.session_state.messages.append({"role": "user", "content": prompt})
58
+
59
+ # Display user message in chat message container
60
+ with st.chat_message("user"):
61
+ st.markdown(prompt)
62
+
63
+ # Prepare the request data
64
+ request_data = {"model": "dolphin", "messages": st.session_state.messages}
65
+
66
+ # Get response from fireworks.ai
67
+ response_content = get_response(request_data["messages"])
68
+
69
+ # Display assistant message in chat message container
70
+ with st.chat_message("assistant"):
71
+ st.markdown(response_content)
72
+
73
+ # Add assistant response to chat history
74
+ st.session_state.messages.append({"role": "assistant", "content": response_content})