Ashish Rai commited on
Commit
ed2f560
·
1 Parent(s): 50da3d0

add Anthropic and OpenAI API

Browse files
Files changed (3) hide show
  1. .gitignore +17 -0
  2. app.py +107 -59
  3. prompts.py +36 -0
.gitignore ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # standard python ignores
2
+ *.pyc
3
+ __pycache__
4
+
5
+ # virtualenv
6
+ venv/
7
+ .venv3/
8
+
9
+ # IDE files
10
+ .idea/
11
+ .vscode/
12
+
13
+ # macOS files
14
+ .DS_Store
15
+
16
+ # Environment
17
+ .env
app.py CHANGED
@@ -1,63 +1,111 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
+ import anthropic
2
+ from openai import OpenAI
3
  import gradio as gr
4
+ import os
5
+ from prompts import codebook
6
+
7
+ _PROMPT_STYLES = [prompt['prompt_style'] for prompt in codebook]
8
+
9
+
10
+ # Function to call Claude API
11
+ def call_claude_api(input_text, prompt):
12
+ api_key = os.environ.get("ANTHROPIC_API_KEY")
13
+ client = anthropic.Anthropic(api_key=api_key)
14
+
15
+ responses = []
16
+ messages = []
17
+ print("Using prompt style: ", prompt['prompt_style'])
18
+
19
+ for prompt_message in prompt['prompts']:
20
+ message = {"role": "user", "content": prompt_message}
21
+ messages.append(message)
22
+
23
+ response = client.messages.create(
24
+ model="claude-3-5-sonnet-20240620",
25
+ max_tokens=1024,
26
+ temperature=0,
27
+ system=prompt['system_prompt'] + input_text,
28
+ messages=messages
29
+ )
30
+
31
+ messages.append({
32
+ "role": "assistant",
33
+ "content": response.content[0].text
34
+ })
35
+ responses.append(response.content[0].text)
36
+
37
+ return responses
38
+
39
+
40
+ def call_openai_api(input_text, prompt):
41
+ api_key = os.environ.get("OPENAI_API_KEY")
42
+ org_id = os.environ.get("OPENAI_ORG_ID")
43
+ project_id = os.environ.get("OPENAI_PROJECT_ID")
44
+
45
+ client = OpenAI(
46
+ api_key=api_key,
47
+ organization=org_id,
48
+ project=project_id
49
+ )
50
+
51
+ responses = []
52
+ messages = [{
53
+ "role": "system", "content": prompt['system_prompt'] + input_text
54
+ }]
55
+ print("Using prompt style: ", prompt['prompt_style'])
 
 
 
 
 
 
56
 
57
+ for prompt_message in prompt['prompts']:
58
+ message = {"role": "user", "content": prompt_message}
59
+ messages.append(message)
60
+
61
+ response = client.chat.completions.create(
62
+ model="gpt-4o-mini",
63
+ max_tokens=1024,
64
+ temperature=0,
65
+ messages=messages
66
+ )
67
+
68
+ messages.append({
69
+ "role": "assistant",
70
+ "content": response.choices[0].message.content
71
+ })
72
+ responses.append(response.choices[0].message.content)
73
+
74
+ return responses
75
+
76
+
77
+ def process_file(text_file, prompt_style_key):
78
+ with open(text_file.name, "r") as fd:
79
+ content = fd.read()
80
+
81
+ for prompt in codebook:
82
+ if prompt['prompt_style'] == prompt_style_key:
83
+ selected_prompt = prompt
84
+ break
85
+
86
+ # responses = call_claude_api(content, prompt=selected_prompt)
87
+ responses = call_openai_api(content, prompt=selected_prompt)
88
+ sections = selected_prompt["sections"]
89
+
90
+ final_response = ''
91
+ for section, response in zip(sections, responses):
92
+ final_response += f"# {section}\n{response}\n\n"
93
+ return final_response
94
+
95
+
96
+ # Gradio Components
97
+ file_upload = gr.File(label="Upload your paper (only .txt files supported)")
98
+ dropdown = gr.Dropdown(choices=list(_PROMPT_STYLES), label="Research domain", value=_PROMPT_STYLES[0])
99
+ output = gr.Textbox(label="Critique")
100
+
101
+ # Gradio Interface
102
+ iface = gr.Interface(
103
+ fn=process_file,
104
+ inputs=[file_upload, dropdown],
105
+ outputs=output,
106
+ title="Alignment Research Critiquer",
107
+ description="An LLM-based app to critique AI alignment research papers."
108
+ )
109
 
110
  if __name__ == "__main__":
111
+ iface.launch()
prompts.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ codebook = [
2
+ {
3
+ "prompt_style": "alignment (general)",
4
+ "system_prompt": "In this conversation, you will play the role of an experienced researcher critiquing research papers within the field of AI technical alignment. In your role, your priority is to be particularly critical and look out for holes in the authors’ arguments or empirical work for the purpose of helping early career AI safety researchers. Be direct, to the point, and concise. Here is the paper: ",
5
+ "prompts": [
6
+ "First, please provide an overview of the paper's claims. Extract text from the paper as evidence. Explain your reasoning for each step in your response.",
7
+ "Now, provide an overview of the paper's study design and methodology. Include an answer to the following question: Are the compute / data requirements stated and comparable across methods and benchmarks? If not, is this discussed in the paper? Extract text from the paper as evidence. Explain your reasoning for each step in your response.",
8
+ "Provide an overview of the results, discussion, limitations, and future directions for research from the paper. Include an answer to the question “Are the claims following logically throughout the paper? Extract text from the paper as evidence. Explain your reasoning for each step in your response.",
9
+ "Next, critique the study design and methodology. Include a comprehensive answer to questions, “What evidence do the authors provide that their method performs well? “Does the empirical evidence support the claims? Provide an overview of the specific strengths of the paper. Extract text from the paper as evidence. Explain your reasoning for each step in your response.",
10
+ "Now, provide a comprehensive list of the weaknesses, how it weakens the evidence of the paper's claim, and why it does so. Include an answer to the following question in detail, “Is the method tested on a sufficiently broad set of benchmarks? “ Extract text from the paper as evidence. Explain your reasoning for each step in your response.",
11
+ "Critique the Results, Discussion, and limitations of the paper. Include an answer for the question 'Are the results statistically significant and does the evaluation protocol follow best practices (reporting confidence intervals, significance testing)?'",
12
+ "Now, please provide a comprehensive list of possible research directions for AI safety researchers. Include numerous, specific and narrowly scoped technical research questions to for exploratory analysis in each research direction. Choose research questions that will most benefit the field of AI Safety. Explain your reasoning for each step."
13
+ ],
14
+ "sections": [
15
+ "Paper Overview and Claims",
16
+ "Study Design and Methodology",
17
+ "Results and Discussion",
18
+ "Methodology Critique",
19
+ "Weaknesses and Evidence Impact",
20
+ "Results and Statistical Significance",
21
+ "Future Research Directions"
22
+ ]
23
+ },
24
+ {
25
+ "prompt_style": "mech_interp",
26
+ "system_prompt": "Here is the paper: ",
27
+ "prompts": [
28
+ "I've attached a paper on mechanistic interpretability of large language models. I'm submitting the paper soon and would like to practice responding to peer review, please give me a high quality response in the style of a thoughtful and careful reviewer of a prestigious conference like ICLR / ICML / NeurIPS about the strengths and weaknesses of the paper, and what can be improved about it.",
29
+ "Please also generate a low-effort and hostile response that misses the point of the paper, in the style of the proverbial 'reviewer 2'. I want practice preparing myself for the worst."
30
+ ],
31
+ "sections": [
32
+ "High Quality Review",
33
+ "Low Quality Review"
34
+ ]
35
+ }
36
+ ]