Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb441bd

Browse files
committed
Initial commit
0 parents commit fb441bd

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

‎.DS_Store

6 KB
Binary file not shown.

‎.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

‎README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# OpenAi Function Calling Use Examples
2+
3+
A simple example that demonstrates how to use the function call feature of the OpenAI API
4+
5+
## Libraries that need to be installed
6+
7+
1. openai
8+
2. streamlit
9+
3. python-dotenv
10+
11+
## installation
12+
13+
1. Clone the current repository
14+
15+
```shell
16+
$ git clone https://github.com/Simoon-F/openai-function-calling-use-exaples
17+
```
18+
19+
2. Enter the project root directory
20+
21+
```shell
22+
$ cd openai-function-calling-use-exaples
23+
```
24+
25+
3. Create the `.env` file and add your api key
26+
27+
4. Run Project
28+
```shell
29+
$ streamlit run main.py
30+
```
31+
32+
[function-calling-and-other-api-updates](https://openai.com/blog/function-calling-and-other-api-updates)

‎main.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /