Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

Commit

Permalink
Merge pull request #137 from openchatai/feature/response-slim-compres…
Browse files Browse the repository at this point in the history
…sion

Context size reduction
  • Loading branch information
codebanesr authored Oct 9, 2023
2 parents 2444f52 + bec94e8 commit aa8d532
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion llm-server/routes/workflow/extractors/example_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ def generate_example_with_format(property_schema: Dict[str, Any]) -> Any:


def gen_ex_from_schema(schema: Any) -> Any:
return json.dumps(generate_example_json(schema))
return json.dumps(generate_example_json(schema), separators=(',', ':'))
2 changes: 1 addition & 1 deletion llm-server/routes/workflow/extractors/extract_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def gen_body_from_schema(
HumanMessage(content="User input: {}".format(text)),
HumanMessage(content="prev api responses: {}".format(prev_api_response)),
HumanMessage(
content="Given the provided information, generate the appropriate JSON payload to use as body for the API request."
content="Given the provided information, generate the appropriate minified JSON payload to use as body for the API request."
),
]
result = chat(messages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def transform_api_response_from_schema(server_url: str, responseText: str) -> st
)
),
HumanMessage(
content="Analyze the provided API responses and extract only the essential fields required for subsequent API interactions. Disregard any non-essential attributes such as CSS or color-related data. If there are generic fields like 'id,' provide them with more descriptive names in your response. Format your response as a JSON object with clear and meaningful keys that map to their respective values from the API response."
content="Analyze the provided API responses and extract only the essential fields required for subsequent API interactions. Disregard any non-essential attributes such as CSS or color-related data. If there are generic fields like 'id,' provide them with more descriptive names in your response. Format your response as a minified JSON object with clear and meaningful keys that map to their respective values from the API response."
),
]

Expand Down
6 changes: 3 additions & 3 deletions llm-server/routes/workflow/generate_openapi_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ def generate_openapi_payload(
{}
if not api_info.path_params["properties"]
else gen_params_from_schema(
json.dumps(api_info.path_params), text, prev_api_response
json.dumps(api_info.path_params, separators=(',', ':')), text, prev_api_response
)
)
api_info.query_params = (
{}
if not api_info.query_params["properties"]
else gen_params_from_schema(
json.dumps(api_info.query_params), text, prev_api_response
json.dumps(api_info.query_params, separators=(',', ':')), text, prev_api_response
)
)

if api_info.body_schema:
example = gen_ex_from_schema(api_info.body_schema)
api_info.body_schema = gen_body_from_schema(
json.dumps(api_info.body_schema), text, prev_api_response, example
json.dumps(api_info.body_schema, separators=(',', ':')), text, prev_api_response, example
)

else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def hasSingleIntent(swagger_doc: Any, user_requirement: str) -> BotMessage:
)
messages = [
SystemMessage(
content="You serve as an AI co-pilot tasked with identifying the correct sequence of API calls necessary to execute a user's action. If the user is asking you to perform a `CRUD` operation, provide the list of operation ids of api calls needed in the `ids` field of the json. `bot_message` should consist of a straightforward sentence, free from any special characters. Your response MUST be a valid json"
content="You serve as an AI co-pilot tasked with identifying the correct sequence of API calls necessary to execute a user's action. If the user is asking you to perform a `CRUD` operation, provide the list of operation ids of api calls needed in the `ids` field of the json. `bot_message` should consist of a straightforward sentence, free from any special characters. Your response MUST be a valid minified json"
),
HumanMessage(
content="Here's a list of api summaries {}.".format(summaries),
Expand Down
8 changes: 4 additions & 4 deletions llm-server/routes/workflow/utils/fetch_swagger_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def fetch_swagger_text(swagger_url: str) -> str:
try:
# Try parsing the content as JSON
json_content = json.loads(response.text)
return json.dumps(json_content, indent=2)
return json.dumps(json_content, separators=(',', ':'))
except json.JSONDecodeError:
try:
# Try parsing the content as YAML
yaml_content = yaml.safe_load(response.text)
if isinstance(yaml_content, dict):
return json.dumps(yaml_content, indent=2)
return json.dumps(yaml_content, separators=(',', ':'))
else:
raise Exception("Invalid YAML content")
except ParserError:
Expand All @@ -36,13 +36,13 @@ def fetch_swagger_text(swagger_url: str) -> str:
try:
# Try parsing the content as JSON
json_content = json.loads(content)
return json.dumps(json_content, indent=2)
return json.dumps(json_content, separators=(',', ':'))
except json.JSONDecodeError:
try:
# Try parsing the content as YAML
yaml_content = yaml.safe_load(content)
if isinstance(yaml_content, dict):
return json.dumps(yaml_content, indent=2)
return json.dumps(yaml_content, separators=(',', ':'))
else:
raise Exception("Invalid YAML content")
except ParserError:
Expand Down
2 changes: 1 addition & 1 deletion llm-server/routes/workflow/utils/run_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def run_workflow(

output = {"response": result if not error else "", "error": error}

logging.info("[OpenCopilot] Workflow output %s", json.dumps(output))
logging.info("[OpenCopilot] Workflow output %s", json.dumps(output, separators=(',', ':')))

return output
18 changes: 9 additions & 9 deletions llm-server/utils/make_api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ def make_api_request(
headers,
servers,
) -> Response:
endpoint = replace_url_placeholders(endpoint, path_params)
url = servers[0] + endpoint
# Create a session and configure it with headers
session = requests.Session()
try:
endpoint = replace_url_placeholders(endpoint, path_params)
url = servers[0] + endpoint
# Create a session and configure it with headers
session = requests.Session()

# Add the "Content-Type" header with the value "application/json" to the headers
headers["Content-Type"] = "application/json"
# Add the "Content-Type" header with the value "application/json" to the headers
headers["Content-Type"] = "application/json"

if headers:
session.headers.update(headers)
try:
if headers:
session.headers.update(headers)
# Perform the HTTP request based on the request type
if method == "GET":
response = session.get(url, params=query_params)
Expand Down

0 comments on commit aa8d532

Please sign in to comment.