tregu0458 commited on
Commit
1517e67
·
verified ·
1 Parent(s): 29036c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -18
app.py CHANGED
@@ -36,35 +36,72 @@ def fetch_message(df, num, direction):
36
  def create_prompt(instruction, num, tone, limit):
37
  return f"{instruction}\n## 制約条件\n- {num}文字以上\n- {tone}\n- {limit}\n"
38
 
 
39
  def respond(message, chat_history, system_prompt, model, api_key, temperature, max_tokens):
40
  try:
41
  client = anthropic.Anthropic(api_key=api_key)
42
- messages = (
43
- {"role": "system", "content": [{"type": "text", "text": system_prompt}]},
44
- *[
45
- {"role": "user", "content": [{"type": "text", "text": item[0]}]},
46
- {"role": "assistant", "content": [{"type": "text", "text": item[1]}]}
47
- ]
48
- for item in chat_history
49
- )
50
- messages.append({"role": "user", "content": [{"type": "text", "text": message}]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
 
 
 
 
 
 
 
 
 
 
 
52
  bot_message = ""
53
- chat_history.append((message, bot_message))
54
  with client.messages.stream(
55
  messages=messages,
 
56
  model=model,
57
  max_tokens=max_tokens,
58
  temperature=temperature,
59
- ) as stream:
60
- for text in stream.text_stream:
61
- bot_message += text
62
- chat_history[-1] = (message, bot_message)
63
- yield chat_history
64
  except Exception as e:
65
- bot_message = str(e)
66
- chat_history.append((message, bot_message))
67
- return chat_history
 
68
 
69
  with gr.Blocks() as demo:
70
  gr.Markdown("# 生成AIのプロンプト術を学ぶクイックスタートガイド")
 
36
  def create_prompt(instruction, num, tone, limit):
37
  return f"{instruction}\n## 制約条件\n- {num}文字以上\n- {tone}\n- {limit}\n"
38
 
39
+
40
  def respond(message, chat_history, system_prompt, model, api_key, temperature, max_tokens):
41
  try:
42
  client = anthropic.Anthropic(api_key=api_key)
43
+ messages = []
44
+ for item in chat_history:
45
+ if isinstance(item, tuple):
46
+ usermessage = item[0]
47
+ assistantmessage = item[1]
48
+ elif isinstance(item, list):
49
+ usermessage = item[0]
50
+ assistantmessage = item[1]
51
+ if isinstance(usermessage, str) and usermessage.startswith("[('"):
52
+ usermessage = eval(usermessage)[0][1]
53
+ assistantmessage = eval(assistantmessage)[0][1]
54
+ else:
55
+ continue
56
+
57
+ messages.append({
58
+ "role": "user",
59
+ "content": [
60
+ {
61
+ "type": "text",
62
+ "text": usermessage
63
+ }
64
+ ]
65
+ })
66
+ messages.append({
67
+ "role": "assistant",
68
+ "content": [
69
+ {
70
+ "type": "text",
71
+ "text": assistantmessage
72
+ }
73
+ ]
74
+ })
75
 
76
+ messages.append(
77
+ {
78
+ "role": "user",
79
+ "content": [
80
+ {
81
+ "type": "text",
82
+ "text": message
83
+ }
84
+ ]
85
+ }
86
+ )
87
  bot_message = ""
88
+ chat_history.append((message,bot_message))
89
  with client.messages.stream(
90
  messages=messages,
91
+ system=system_prompt,
92
  model=model,
93
  max_tokens=max_tokens,
94
  temperature=temperature,
95
+ )as stream:
96
+ for text in stream.text_stream:
97
+ bot_message += text
98
+ chat_history[-1]=(message, bot_message)
99
+ yield chat_history
100
  except Exception as e:
101
+ bot_message = str(e)
102
+ chat_history.append((message, bot_message))
103
+ return chat_history
104
+
105
 
106
  with gr.Blocks() as demo:
107
  gr.Markdown("# 生成AIのプロンプト術を学ぶクイックスタートガイド")