# Tags **Software** : [[Python]] [[ChatGPT]] **Functions** used : **Date** : 23-March-2025 # Description - In this 2 part video series i will first show you how to make a call to the OpenAI chatgpt API and get a reply. In the following video we will use the code and implement the API Call in Power Query. 📞🤖 # Video Link <iframe width="560" height="315" src="https://www.youtube.com/embed/DDa8S2lc61w?si=3VyB4k7SUYjP9Z_r" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> [GITHUB File Link](https://github.com/jbotes/powerbiTutorials/blob/main/01_PQ_YT_Notebook.ipynb) # M-Query ```Python import openai import os API_Key = "xxxxx" openai.api_key = API_Key openai.api_key = os.getenv("OPENAI_API_KEY") response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages = [{"role":"user","content": "Hello World!"}], temperature=1, max_tokens=600, top_p=1, frequency_penalty=0, presence_penalty=0 ) def ask_chatgpt(messages): model_response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, temperature=1, max_tokens=600, top_p=1, frequency_penalty=0, presence_penalty=0 ) return model_response["choices"][0]["message"]["content"] prompt_role='You are a Financial Analyst who needs to provide Market updates for your clients. Context : the daily market information is provided to you daily. Task : Summarize in 140 words the following Market news for a twitter post. The Market news to summarize:' Commentary = 'The JSE ended weaker on Tuesday, reversing some of the gains from the previous day. The market was pulled down by resource and industrial stocks. On the currency front, the rand traded firmer against the dollar, mainly supported by recent hawkish remarks from the South African Reserve Bank ahead of a crucial budget statement. The JS E All Share and blue-chip Top 40 Index decreased by 0.3% and 0.6%. European markets closed broadly stronger as investors reacted to a slew of earnings updates and economic data from the region. The UK’s FTSE 100 declined by 0.1%, while Germany’s DAX and France’s CAC 40 advanced by 0.6% and 0.9% respectively. US stocks moved mostly higher over the course of the trading session following some early weakness. Selling pressure faded shortly after the start of trading, as investors seemed reluctant to make significant moves ahead of the Federal Reserves monetary policy announcement on Wednesday. The S&P 500 and the Nasdaq Composite jumped by 0.6% and 0.5%, while the Dow Jones Industrial Average improved by 0.4%. Asian markets are up this morning. The Nikkei, the Hang Seng and the Shanghai Composite are 2.1%, 0.1% and 0.3% in the green.' def assist_analyst(prole, MainComment): prompt = f'{prole}{MainComment}' return ask_chatgpt([{"role": "user", "content": prompt}]) assist_analyst(prompt_role, Commentary) ``` ## Optional Tags **Tags** : **Technique** : [[Python in Power Query]]