|
| 1 | +import streamlit as st |
| 2 | +import openai |
| 3 | +import json |
| 4 | +import requests as rq |
| 5 | +from dotenv import dotenv_values |
| 6 | + |
| 7 | + |
| 8 | +class SkillsList: |
| 9 | + def __init__(self): |
| 10 | + """ |
| 11 | + The api of Gaud Open Platform is used here. |
| 12 | + https://lbs.amap.com/api/webservice/guide/api/weatherinfo |
| 13 | + """ |
| 14 | + self.weather_api_url = "https://restapi.amap.com/v3/weather/weatherInfo" |
| 15 | + self.amap_api_key = env['AMAP_API_KEY'] |
| 16 | + |
| 17 | + def query_city_weather(self, city): |
| 18 | + """ |
| 19 | + Query the weather temperature of the city. |
| 20 | +
|
| 21 | + Args: |
| 22 | + city (str): Cities that should be queried. |
| 23 | + """ |
| 24 | + params = { |
| 25 | + "key": self.amap_api_key, |
| 26 | + "city": city, |
| 27 | + "output": "json", |
| 28 | + "extensions": "all", |
| 29 | + } |
| 30 | + |
| 31 | + response = rq.get(self.weather_api_url, params=params) |
| 32 | + |
| 33 | + response.raise_for_status() |
| 34 | + |
| 35 | + weather_data = response.json() |
| 36 | + |
| 37 | + for item in weather_data['forecasts']: |
| 38 | + st.markdown(f"{item['province'] + item['city']} is as follows:") |
| 39 | + for cast in item['casts']: |
| 40 | + st.markdown( |
| 41 | + f"**{cast['date']}** :`dayweather`:{cast['dayweather']},`nightweather`:{cast['nightweather']}, `daytemp`: {cast['daytemp']}, `nighttemp`:{cast['nighttemp']}") |
| 42 | + |
| 43 | + |
| 44 | +def call_gpt(user_input): |
| 45 | + """ |
| 46 | + Make a ChatCompletion API call to OpenAI GPT-3.5-turbo model. |
| 47 | +
|
| 48 | + Args: |
| 49 | + user_input (str): The user's prompt or input text. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + str: The generated response from the API call. |
| 53 | + """ |
| 54 | + messages = [{"role": "user", "content": user_input}] |
| 55 | + |
| 56 | + function = { |
| 57 | + "name": "query_city_weather", |
| 58 | + "description": "query weather temperature", |
| 59 | + "parameters": { |
| 60 | + "type": "object", |
| 61 | + "properties": { |
| 62 | + "city": { |
| 63 | + "type": "string", |
| 64 | + "description": "The city", |
| 65 | + }, |
| 66 | + }, |
| 67 | + "required": ["city"], |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + completion = openai.ChatCompletion.create( |
| 72 | + model="gpt-3.5-turbo-0613", |
| 73 | + messages=messages, |
| 74 | + functions=[function], |
| 75 | + function_call="auto", |
| 76 | + ) |
| 77 | + return completion.choices[0].message |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + st.title("Small assistant") |
| 82 | + |
| 83 | + env = dotenv_values() |
| 84 | + openai.api_key = env['OPENAI_API_KEY'] |
| 85 | + |
| 86 | + skills_list_obj = SkillsList() |
| 87 | + |
| 88 | + prompt = st.text_input("Enter your command:") |
| 89 | + |
| 90 | + if prompt: |
| 91 | + reply_content = call_gpt(prompt) |
| 92 | + |
| 93 | + reply_content_dict = reply_content.to_dict() |
| 94 | + method_name = reply_content_dict['function_call']['name'] |
| 95 | + method_args = reply_content_dict['function_call']['arguments'] |
| 96 | + |
| 97 | + print(method_name, method_args) |
| 98 | + |
| 99 | + method_args_dict = json.loads(method_args) |
| 100 | + |
| 101 | + method = getattr(skills_list_obj, method_name) |
| 102 | + method(method_args_dict['city']) |
0 commit comments