preamble
Since Chatgpt came out of nowhere, various smart tools have emerged, chatting, drawing, video and various other tools to help many people work efficiently. As a developer, the current common applications include code autofill, chat assistant, and so on.
These are the use of the tool level, there is no big model and the daily coding of the front and back end process combined with the use of the entry point? Today we share an example of using a big model to automatically call api interfaces, which can be used as another scenario of using big models in development.
Interface Description
Sample interfaces are as follows, an interface for the calculation of eight characters (only as an example, not a feudal superstition)
Request Interface:[//v1/Bazi/paipan](//v1/Bazi/paipan)
Request method: POST
Request body.
{
"api_key": "xxxxx",
"name": "zhangsan",
"sex": 0,
"type": 1,
"month": 10, "day": 10, "month": 10
"hours": 22, "minute": 0
"minute": 0
}
api_key needs to be requested on the website
Request Example:
Large models extract information to send requests
The interface information as above, the normal development process is to construct the url, body, and then send the request. Use the big model first text information extraction into the body of the request body, using the langchain framework to complete.
The principle is very simple: create the right cue words and let the big model extract the key fields in the incoming content and return them in json format.
Examples are as follows
import os
import requests
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_openai import ChatOpenAI
["OPENAI_API_KEY"] = "sk-eCWGtrwU14E60A45E8a2T3BlbKFJ18b59d41Fdb748828B0f"
["OPENAI_API_BASE"] = "/v1"
def fun(query).
url = f"//v1/Bazi/cesuan"
# Create a big model prompt that extracts the content of the text
prompt = ChatPromptTemplate.from_template(
"""You are a parameter query helper that finds out the relevant parameters based on user input and returns them in json format.
The JSON fields are as follows:
- "api_key": "K0I5WCmce7jlMZzTw7vi1xsn0",
- "name": "name",
- "sex": "Sex, 0 means male, 1 means female, based on name",
- "type": "Calendar type, 0 for lunar, 1 for kilometer, default 1",
- "year": "Year of birth Example: 1998",
- "month": "Month of birth Example: 8",
- "hours": "Hours of birth Example: 14", "minute": "0".
- "minute": "0".
If the relevant parameters are not found, the user needs to be reminded to tell you about them, and to return only the data structure, without any other comments.
user input:{query}""")
chain = prompt | ChatOpenAI(temperature=0) | JsonOutputParser()
data = ({"query": query})
print(f "Interface body formatting result: {data}")
result = (url, data)
print(f "Interface key information: {()['data']['bazi_info']}")
try.
content = ()
res = f "The eight characters are: {content['data']['bazi_info']['bazi']}"
print(res)
except Exception as e.
print(f "Eight query failed, returned data exception: {e}")
print(f "Query failed, data returned exceptionally: {e}")
print("Interface request status exception")
if __name__ == "__main__".
query = "October 10, 2000 at 22:00"
fun(query)
Results:
Interface body formatting result: {'year': '2000', 'month': '10', 'day': '10', 'hours': '22', 'api_key': 'K0I5WCmce7jlMZzTw7vi1xsn0', 'name': 'name', 'sex': 'gender, 0 means male, 1 means female, judge by name', 'type': 'Calendar type, 0 lunar, 1 Gregorian, default 1', 'minute': '0'}
Interface keywords: {'kw': 'huxuhai', 'tg_cg_god': ['positive wealth', 'robber wealth', 'yen', 'partial wealth'], 'bazi': 'geng chen cingxu dingmao xinhai', 'na_yin': 'white wax gold'}
Eight characters are: gengchen cingxu dingmao xinhai
Reproduce the necessary preparations:
- Install langchain
- Domestic openai agent
- Website api_key
analyze
The first step is to construct the cue, which is the soul of the process. Typically, cue words are commonly used in the CRISPE framework, which provides the model with detailed background, task objectives, and output formatting requirements.
CR: capacity and role Capacity and Role | What role the model is expected to play and what capabilities the role has | You're a parameter query assistant. |
---|---|---|
I: INSIGHT Insight | Background information on which the completion of the mandate depends | |
S: statement command | What the model is expected to do, core keywords and objectives of the task | Find relevant parameters based on user input |
P: Personality | The style or manner in which you want the model to be output | and return it in json format |
E: experiment Try | Requires the model to provide multiple answers and the number of task output results |
Cue word in this example:
# Create big model prompts to extract the content of the text
prompt = ChatPromptTemplate.from_template(
"""You are a parameter query helper that finds out the relevant parameters based on user input and returns them in json format.
The JSON fields are as follows:
- "api_key": "K0I5WCmce7jlMZzTw7vi1xsn0",
- "name": "name",
- "sex": "Sex, 0 means male, 1 means female, based on name",
- "type": "Calendar type, 0 for lunar, 1 for kilometer, default 1",
- "year": "Year of birth Example: 1998",
- "month": "Month of birth Example: 8",
- "hours": "Hours of birth Example: 14", "minute": "0".
- "minute": "0".
If the relevant parameters are not found, the user needs to be reminded to tell you about them, and to return only the data structure, without any other comments.
user input:{query}""")
Calling the process:
chain = prompt | ChatOpenAI(temperature=0) | JsonOutputParser()
data = ({"query": query})
print(f "Interface body formatting result: {data}")
With the appropriate cue words, the incoming information will beLetter dated 10 October 2000 from the Chargé d'affaires a.i. of the Permanent Mission of
extracted and assembled into a single result. Among themChatOpenAI(temperature=0)
It is for the big model not to think out of the box and generate content strictly according to the cue words to avoid inaccurate results.JsonOutputParser()
Returned results json formatted.
{'year': '2000', 'month': '10', 'day': '10', 'hours': '22', 'api_key': 'K0I5WCmce7jlMZzTw7vi1xsn0', 'name': 'Name', 'sex': 'Sex, 0 means male, 1 means female, based on name', 'type': 'calendar type, 0 lunar calendar, 1 Gregorian calendar, default 1', 'minute': '0'} 'type': 'Calendar type, 0 lunar, 1 Gregorian, default 1', 'minute': '0'}
Then userequests
Sends a post request and parses the returned results.
wrap-up
This is a very simple big model application , the main point is to use the big model to organize the parameters of the interface call , including fields and formats , to replace the manual coding implementation . Personally, I think it can slightly improve efficiency in complex or frequently changing scenarios.