kenken999 commited on
Commit
b8cc7e0
·
1 Parent(s): 8588faf
mysite/interpreter/interpreter.py CHANGED
@@ -18,44 +18,150 @@ def set_environment_variables():
18
  os.environ["MODEL_NAME"] = "llama3-8b-8192"
19
  os.environ["LOCAL_MODEL"] = "true"
20
 
21
- # Set the environment variable.
22
- def chat_with_interpreter(
23
- message, history=None, a=None, b=None, c=None, d=None
24
- ): # , openai_api_key):
25
- # Set the API key for the interpreter
26
- # interpreter.llm.api_key = openai_api_key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  if message == "reset":
28
  interpreter.reset()
29
  return "Interpreter reset", history
 
30
  full_response = ""
31
- # add_conversation(history,20)
 
 
 
 
 
32
  user_entry = {"role": "user", "type": "message", "content": message}
33
- #messages.append(user_entry)
34
- # Call interpreter.chat and capture the result
35
- messages = []
36
- recent_messages = history[-20:]
37
- for conversation in recent_messages:
38
- user_message = conversation[0]
39
- user_entry = {"role": "user", "content": user_message}
40
- messages.append(user_entry)
41
- assistant_message = conversation[1]
42
- assistant_entry = {"role": "assistant", "content": assistant_message}
43
- messages.append(assistant_entry)
44
 
45
- user_entry = {"role": "user", "content": message}
46
- messages.append(user_entry)
47
- #system_prompt = {"role": "system", "content": "あなたは日本語の優秀なアシスタントです。"}
48
- #messages.insert(0, system_prompt)
 
 
49
 
50
- for chunk in interpreter.chat(messages, display=False, stream=True):
51
- # print(chunk)
52
- # output = '\n'.join(item['content'] for item in result if 'content' in item)
53
- full_response = format_response(chunk, full_response)
54
- yield full_response # chunk.get("content", "")
55
 
56
- yield full_response + rows # , history
57
  return full_response, history
58
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  async def completion(message: str, history, c=None, d=None, prompt="あなたは日本語の優秀なアシスタントです。"):
60
  from groq import Groq
61
  client = Groq(api_key=os.getenv("api_key"))
 
18
  os.environ["MODEL_NAME"] = "llama3-8b-8192"
19
  os.environ["LOCAL_MODEL"] = "true"
20
 
21
+ import gradio as gr
22
+ from mysite.libs.utilities import chat_with_interpreter, completion, process_file,no_process_file
23
+ from interpreter import interpreter
24
+ import mysite.interpreter.interpreter_config # インポートするだけで設定が適用されます
25
+ import duckdb
26
+
27
+ def format_response(chunk, full_response):
28
+ # Message
29
+ if chunk["type"] == "message":
30
+ full_response += chunk.get("content", "")
31
+ if chunk.get("end", False):
32
+ full_response += "\n"
33
+
34
+ # Code
35
+ if chunk["type"] == "code":
36
+ if chunk.get("start", False):
37
+ full_response += "```python\n"
38
+ full_response += chunk.get("content", "").replace("`", "")
39
+ if chunk.get("end", False):
40
+ full_response += "\n```\n"
41
+
42
+ # Output
43
+ if chunk["type"] == "confirmation":
44
+ if chunk.get("start", False):
45
+ full_response += "```python\n"
46
+ full_response += chunk.get("content", {}).get("code", "")
47
+ if chunk.get("end", False):
48
+ full_response += "```\n"
49
+
50
+ # Console
51
+ if chunk["type"] == "console":
52
+ if chunk.get("start", False):
53
+ full_response += "```python\n"
54
+ if chunk.get("format", "") == "active_line":
55
+ console_content = chunk.get("content", "")
56
+ if console_content is None:
57
+ full_response += "No output available on console."
58
+ if chunk.get("format", "") == "output":
59
+ console_content = chunk.get("content", "")
60
+ full_response += console_content
61
+ if chunk.get("end", False):
62
+ full_response += "\n```\n"
63
+
64
+ # Image
65
+ if chunk["type"] == "image":
66
+ if chunk.get("start", False) or chunk.get("end", False):
67
+ full_response += "\n"
68
+ else:
69
+ image_format = chunk.get("format", "")
70
+ if image_format == "base64.png":
71
+ image_content = chunk.get("content", "")
72
+ if image_content:
73
+ image = Image.open(BytesIO(base64.b64decode(image_content)))
74
+ new_image = Image.new("RGB", image.size, "white")
75
+ new_image.paste(image, mask=image.split()[3])
76
+ buffered = BytesIO()
77
+ new_image.save(buffered, format="PNG")
78
+ img_str = base64.b64encode(buffered.getvalue()).decode()
79
+ full_response += f"![Image](data:image/png;base64,{img_str})\n"
80
+
81
+ return full_response
82
+
83
+ import sqlite3
84
+ from datetime import datetime
85
+
86
+ # SQLiteの設定
87
+ db_name = "chat_history.db"
88
+
89
+ def initialize_db():
90
+ conn = sqlite3.connect(db_name)
91
+ cursor = conn.cursor()
92
+ cursor.execute("""
93
+ CREATE TABLE IF NOT EXISTS history (
94
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
95
+ role TEXT,
96
+ type TEXT,
97
+ content TEXT,
98
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
99
+ )
100
+ """)
101
+ conn.commit()
102
+ conn.close()
103
+
104
+ def add_message_to_db(role, message_type, content):
105
+ conn = sqlite3.connect(db_name)
106
+ cursor = conn.cursor()
107
+ cursor.execute("INSERT INTO history (role, type, content) VALUES (?, ?, ?)", (role, message_type, content))
108
+ conn.commit()
109
+ conn.close()
110
+
111
+ def get_recent_messages(limit=20):
112
+ conn = sqlite3.connect(db_name)
113
+ cursor = conn.cursor()
114
+ cursor.execute("SELECT role, type, content FROM history ORDER BY timestamp DESC LIMIT ?", (limit,))
115
+ messages = cursor.fetchall()
116
+ conn.close()
117
+ return messages[::-1] # 最新の20件を取得して逆順にする
118
+
119
+ def format_responses(chunk, full_response):
120
+ # This function will format the response from the interpreter
121
+ return full_response + chunk.get("content", "")
122
+
123
+ def chat_with_interpreter(message, history, a=None, b=None, c=None, d=None):
124
  if message == "reset":
125
  interpreter.reset()
126
  return "Interpreter reset", history
127
+
128
  full_response = ""
129
+ recent_messages = get_recent_messages()
130
+
131
+ for role, message_type, content in recent_messages:
132
+ entry = {"role": role, "type": message_type, "content": content}
133
+ interpreter.messages.append(entry)
134
+
135
  user_entry = {"role": "user", "type": "message", "content": message}
136
+ interpreter.messages.append(user_entry)
137
+ add_message_to_db("user", "message", message)
 
 
 
 
 
 
 
 
 
138
 
139
+ for chunk in interpreter.chat(message, display=False, stream=True):
140
+ if isinstance(chunk, dict):
141
+ full_response = format_response(chunk, full_response)
142
+ else:
143
+ raise TypeError("Expected chunk to be a dictionary")
144
+ yield full_response
145
 
146
+ assistant_entry = {"role": "assistant", "type": "message", "content": full_response}
147
+ interpreter.messages.append(assistant_entry)
148
+ add_message_to_db("assistant", "message", full_response)
 
 
149
 
150
+ yield full_response
151
  return full_response, history
152
 
153
+ # 初期化
154
+ initialize_db()
155
+
156
+
157
+ PLACEHOLDER = """
158
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
159
+ <img src="https://ysharma-dummy-chat-app.hf.space/file=/tmp/gradio/8e75e61cc9bab22b7ce3dec85ab0e6db1da5d107/Meta_lockup_positive%20primary_RGB.jpg" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
160
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
161
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
162
+ </div>
163
+ """
164
+
165
  async def completion(message: str, history, c=None, d=None, prompt="あなたは日本語の優秀なアシスタントです。"):
166
  from groq import Groq
167
  client = Groq(api_key=os.getenv("api_key"))
mysite/routers/fastapi.py CHANGED
@@ -204,7 +204,7 @@ def setup_webhook_routes(app: FastAPI):
204
  """
205
  res_no_process = no_process_file(prompt_for_create_system+res, "gpt_enginner"+ yyyymmddhis)
206
 
207
- #full_response,history = chat_with_interpreter(res)
208
  #send_google_chat_card(webhook_url, f"自動設定開始 {res}", str(full_response), link_text, link_url)
209
 
210
 
 
204
  """
205
  res_no_process = no_process_file(prompt_for_create_system+res, "gpt_enginner"+ yyyymmddhis)
206
 
207
+ full_response,history = chat_with_interpreter(res)
208
  #send_google_chat_card(webhook_url, f"自動設定開始 {res}", str(full_response), link_text, link_url)
209
 
210