Skip to content

Commit

Permalink
update to v0.1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Vokturz committed Mar 19, 2024
1 parent baea203 commit 9c7d349
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 434 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/python_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ jobs:
id-token: write # IMPORTANT: mandatory for sigstore

steps:
- name: Check out code
uses: actions/checkout@v2
- name: Get the version
id: get-version
run: |
VERSION=$(grep -Po 'version = \K.*' setup.cfg)
echo "::set-output name=PACKAGE_VERSION::$VERSION"
- name: Download all the dists
uses: actions/download-artifact@v3
with:
Expand All @@ -81,7 +88,7 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
run: >-
gh release create
'${{ github.ref_name }}'
'${{ steps.get-version.outputs.PACKAGE_VERSION }}'
--repo '${{ github.repository }}'
--notes ""
- name: Upload artifact signatures to GitHub Release
Expand All @@ -92,5 +99,5 @@ jobs:
# sigstore-produced signatures and certificates.
run: >-
gh release upload
'${{ github.ref_name }}' dist/**
'${{ steps.get-version.outputs.PACKAGE_VERSION }}' dist/**
--repo '${{ github.repository }}'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.12] - 19-03-2024
### Changed
- Fix problem with `stream=True`
- Remove `format` parameter: Now it will be always `text`.

## [0.1.11] - 01-03-2024
### Changed
- Remove unused packages
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = judini
version = 0.1.11
version = 0.1.12
author = Daniel Avila
author_email = daniel@judini.ai
description = CodeGPT python package
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="judini",
version="0.1.11",
version="0.1.12",
author="Judini Inc.",
author_email="daniel@judini.ai",
description="CodeGPT python package",
Expand Down
19 changes: 7 additions & 12 deletions src/judini/codegpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def __init__(self,
#######################

def chat_completion(self, agent_id: str, messages: List[Dict[str, str]],
stream: bool = False, format: Literal['json', 'text'] = 'text'
) -> str | Dict[str, str]:
stream: bool = False) -> str:
"""
Initiates a chat with the specified agent and handles the streaming of
responses.
Expand All @@ -63,15 +62,13 @@ def chat_completion(self, agent_id: str, messages: List[Dict[str, str]],
object should have a `role` (which can be 'system', 'user',
or 'assistant') and `content` which is the actual message.
stream: Whether to stream the response or not.
format: The format of the response. Can be either 'json' or 'text'.
Example:
>>> from judini import CodeGPTPlus
>>> codegpt = CodeGPTPlus(api_key, org_id)
>>> agent_id = '00000000-0000-0000-0000-000000000000'
>>> messages = [{'role': 'user', 'content': 'Hello, World!'}]
>>> codegpt.chat_completion(agent_id, messages,
... stream=True, format='text')
>>> codegpt.chat_completion(agent_id, messages, stream=True)
'Hello, World!'
"""

Expand All @@ -81,17 +78,15 @@ def chat_completion(self, agent_id: str, messages: List[Dict[str, str]],
if not agent_id:
raise ValueError('JUDINI: agent_id should not be empty')

if format not in ['json', 'text']:
raise ValueError('JUDINI: format should be either "json"|"text"')


headers = self.headers.copy()
headers['media_type'] = 'text/event-stream'
headers['accept'] = 'text/event-stream'

payload = json.dumps({
"agentId": agent_id,
"messages": messages,
"stream": stream,
"format": "json" # By default always json
"format": "text"
})

response = requests.post(f"{self.base_url}/chat/completions",
Expand All @@ -102,9 +97,9 @@ def chat_completion(self, agent_id: str, messages: List[Dict[str, str]],
+ f' {response.text} {JUDINI_TUTORIAL}')

if stream:
return handle_stream(response, format)
return handle_stream(response)
else:
return handle_non_stream(response, format)
return handle_non_stream(response)


##############
Expand Down
29 changes: 7 additions & 22 deletions src/judini/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@
from typing import List, Dict, Literal, Generator, Any
import requests

def handle_stream(response: requests.Response,
format: Literal['json', 'text']) -> Generator[Any, Any, Any]:
def handle_stream(response: requests.Response) -> Generator[Any, Any, Any]:
try:
for chunk in response.iter_lines():
for chunk in response.iter_content(chunk_size=64, decode_unicode=True):
if chunk:
chunk_str = chunk.decode("utf-8")
data_array = chunk_str.replace('\n','').split('data: ')[1:]
for jd_str in data_array:
if jd_str == '[DONE]':
break
json_data = json.loads(jd_str)
if format == 'json':
yield json_data
elif format == 'text':
for item in json_data['choices']:
yield item['delta']['content']
yield chunk
except Exception as e:
print(f"Error occurred: {e}")
print(f"Error occurred: {e}", chunk)
finally:
response.close()

def handle_non_stream(response: requests.Response,
format: Literal['json', 'text']) -> str | Dict[Any, Any]:
def handle_non_stream(response: requests.Response) -> str:
try:
json_data = response.json()
if format == 'json':
return json_data
elif format == 'text':
return json_data['choices'][0]['message']['content']
text = response.json()
return text
except Exception as e:
print(f"Error occurred: {e}")
finally:
Expand Down
Loading

0 comments on commit 9c7d349

Please sign in to comment.