m-ric HF staff commited on
Commit
acf816d
β€’
1 Parent(s): 02f89fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -50
app.py CHANGED
@@ -36,6 +36,7 @@ search_tool = DuckDuckGoSearchTool()
36
  print("Search tool created.")
37
 
38
  ### CREATE LLM ENGINE ############
 
39
  from openai import OpenAI
40
  from transformers.agents.llm_engine import MessageRole, get_clean_message_list
41
 
@@ -62,21 +63,20 @@ class OpenAIEngine:
62
  )
63
  return response.choices[0].message.content
64
 
65
- from transformers import ReactCodeAgent, ReactJsonAgent, HfEngine
66
-
67
- llm_engine = OpenAIEngine()
68
 
69
  agent = ReactCodeAgent(
70
- llm_engine=llm_engine,
71
  tools=[search_tool],
72
- max_iterations=20
73
  )
74
 
75
  print("Agent initiated")
76
 
77
  topics = "Large Language Models, Tech, GPUs, AI"
78
 
79
- intermediate_report = agent.run(
80
  f"""Give me a complete newsletter in markdown format of the latest developments in these topics: {topics}, with a consistent style and layout with URL.
81
  Each topic should have its markdown-formatted analysis, including a rundown, detailed bullet points,
82
  and a "Why it matters" section. For each topic there should be at least 3 articles with URL
@@ -99,55 +99,13 @@ Also, gather all news at once using a for loop on all topics with google_search,
99
  Make sure that you didn't forget any topic! If no news are found for any specific topic, specify 'No news on this topic were found in the last 24 hours'."""
100
  )
101
 
102
- print("Intermediate report created:")
103
- print(intermediate_report)
104
-
105
- openai_client = OpenAI(
106
- api_key=os.getenv('OPENAI_API_KEY')
107
- )
108
-
109
- messages = [
110
- {
111
- "role": "user",
112
- "content": f"""Below you will find a report about important news in each topic of this list : {topics}.
113
- Reformulate it into proper markdown, taking care to follow this example format for each topic:
114
- '# Top stories in AI today:\\n\\n
115
- - AI takes spotlight in Super Bowl commercials\\n
116
- - Altman seeks TRILLIONS for global AI chip initiative\\n\\n
117
- ## AI takes spotlight in Super Bowl commercials\\n\\n
118
- **url:** https://example.com/story1 \\n\\
119
- **The Rundown:** AI made a splash in this year\'s Super Bowl commercials...\\n\\n
120
- **The details:** (fill this)\\\n\\n
121
- **Why it matters::** (fill this)\\n\\n
122
- ## Altman seeks TRILLIONS for global AI chip initiative\\n\\n
123
- **The Rundown:** OpenAI CEO Sam Altman is reportedly angling to raise TRILLIONS of dollars...\\n\\n'
124
- **The details:** (fill this)\\n\\n
125
- **Why it matters::** (fill this)\\n\\n'
126
- Now go on! MAKE SURE TO INCLUDE ALL TOPICS, DO NOT DELETE ANY INFORMATION: you need to have 10 different headlines 'Top stories in X' in total, one for each topic.
127
- (if no information is to be found on a specific topic, you can put in the content 'No stories found in the last 24 hours for this topic')
128
- Write it as markdown, but no need to put specific tags at the beginning and end like '```markdown'.
129
- Here is the report :
130
- {intermediate_report}
131
- """
132
- }
133
- ]
134
- response = openai_client.chat.completions.create(
135
- model="gpt-4o-mini",
136
- messages=messages,
137
- temperature=0.5,
138
- )
139
- clean_report = response.choices[0].message.content
140
 
141
 
142
  date = datetime.today().strftime('%Y-%m-%d')
143
 
144
- def create_newsletter(date, clean_report):
145
- newsletter = clean_report
146
- upload_newsletter(date, newsletter)
147
-
148
- create_newsletter(date, clean_report)
149
 
150
- print("Clean report pushed to dataset!")
151
 
152
  with gr.Blocks() as demo:
153
  gr.Markdown(f"{date}, topics: {topics}\n### βœ… Newsletter generated! Content below πŸ‘‡")
 
36
  print("Search tool created.")
37
 
38
  ### CREATE LLM ENGINE ############
39
+ # Below is an example of how to build an OpenAI engine:
40
  from openai import OpenAI
41
  from transformers.agents.llm_engine import MessageRole, get_clean_message_list
42
 
 
63
  )
64
  return response.choices[0].message.content
65
 
66
+ # But instead we use HF one, since it's free:
67
+ from transformers import ReactCodeAgent, HfApiEngine
 
68
 
69
  agent = ReactCodeAgent(
70
+ llm_engine=HfApiEngine("meta-llama/Meta-Llama-3.1-70B-Instruct"),
71
  tools=[search_tool],
72
+ max_iterations=10
73
  )
74
 
75
  print("Agent initiated")
76
 
77
  topics = "Large Language Models, Tech, GPUs, AI"
78
 
79
+ report = agent.run(
80
  f"""Give me a complete newsletter in markdown format of the latest developments in these topics: {topics}, with a consistent style and layout with URL.
81
  Each topic should have its markdown-formatted analysis, including a rundown, detailed bullet points,
82
  and a "Why it matters" section. For each topic there should be at least 3 articles with URL
 
99
  Make sure that you didn't forget any topic! If no news are found for any specific topic, specify 'No news on this topic were found in the last 24 hours'."""
100
  )
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
 
104
  date = datetime.today().strftime('%Y-%m-%d')
105
 
106
+ upload_newsletter(date, report)
 
 
 
 
107
 
108
+ print("Report pushed to dataset!")
109
 
110
  with gr.Blocks() as demo:
111
  gr.Markdown(f"{date}, topics: {topics}\n### βœ… Newsletter generated! Content below πŸ‘‡")