Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openapi #287

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modelscope_agent/llm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .base import LLM_REGISTRY, BaseChatModel
from .dashscope import DashScopeLLM, QwenChatAtDS
from .modelscope import ModelScopeChatGLM, ModelScopeLLM
from .openai import OpenAi
from .openai import OpenAi, OpenAPILocal
from .zhipu import GLM4, ZhipuLLM


Expand All @@ -25,5 +25,5 @@ def get_chat_model(model: str, model_server: str, **kwargs) -> BaseChatModel:

__all__ = [
'LLM_REGISTRY', 'BaseChatModel', 'OpenAi', 'DashScopeLLM', 'QwenChatAtDS',
'ModelScopeLLM', 'ModelScopeChatGLM', 'ZhipuLLM', 'GLM4'
'ModelScopeLLM', 'ModelScopeChatGLM', 'ZhipuLLM', 'GLM4', 'OpenAPILocal'
]
6 changes: 4 additions & 2 deletions modelscope_agent/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ def chat(self,
assert len(messages) > 0, 'messages list must not be empty'

if stream:
return self._chat_stream(messages, stop=stop, **kwargs)
return self._chat_stream(
messages=messages, stop=stop, prompt=prompt, **kwargs)
else:
return self._chat_no_stream(messages, stop=stop, **kwargs)
return self._chat_no_stream(
messages=messages, stop=stop, prompt=prompt, **kwargs)

@retry(max_retries=3, delay_seconds=0.5)
def chat_with_functions(self,
Expand Down
55 changes: 46 additions & 9 deletions modelscope_agent/llm/openai.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from typing import Dict, Iterator, List, Optional

import openai
from modelscope_agent.llm.base import BaseChatModel, register_llm
from openai import OpenAI


@register_llm('openai')
Expand All @@ -11,16 +11,17 @@ class OpenAi(BaseChatModel):
def __init__(self, model: str, model_server: str, **kwargs):
super().__init__(model, model_server)

openai.api_base = kwargs.get('api_base',
'https://api.openai.com/v1').strip()
openai.api_key = kwargs.get(
'api_key', os.getenv('OPENAI_API_KEY', default='EMPTY')).strip()
api_base = kwargs.get('api_base', 'https://api.openai.com/v1').strip()
api_key = kwargs.get('api_key',
os.getenv('OPENAI_API_KEY',
default='EMPTY')).strip()
self.client = OpenAI(api_key=api_key, base_url=api_base)

def _chat_stream(self,
messages: List[Dict],
stop: Optional[List[str]] = None,
**kwargs) -> Iterator[str]:
response = openai.ChatCompletion.create(
response = self.client.completions.create(
model=self.model,
messages=messages,
stop=stop,
Expand All @@ -35,7 +36,7 @@ def _chat_no_stream(self,
messages: List[Dict],
stop: Optional[List[str]] = None,
**kwargs) -> str:
response = openai.ChatCompletion.create(
response = self.client.completions.create(
model=self.model,
messages=messages,
stop=stop,
Expand All @@ -49,13 +50,49 @@ def chat_with_functions(self,
functions: Optional[List[Dict]] = None,
**kwargs) -> Dict:
if functions:
response = openai.ChatCompletion.create(
response = self.client.completions.create(
model=self.model,
messages=messages,
functions=functions,
**kwargs)
else:
response = openai.ChatCompletion.create(
response = self.client.completions.create(
model=self.model, messages=messages, **kwargs)
# TODO: error handling
return response.choices[0].message


@register_llm('openapi')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个llm名字和上面重复了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的是openai,这个是openapi

class OpenAPILocal(BaseChatModel):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openapilocal这个名字是指本地起一个openai的模型?还是调用本地vllm?后者的话名字要不要换?


def __init__(self, model: str, model_server: str, **kwargs):
super().__init__(model, model_server)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用户能用这个跑起来llama么?还是只能openapi?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是用这个跑,需要本地先通过vllm拉起一个openai格式的api
这个功能在modelscope中有提供https://www.modelscope.cn/docs/%E6%9C%AC%E5%9C%B0%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1

openai_api_key = 'EMPTY'
openai_api_base = 'http://localhost:8000/v1'
self.client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)

def _chat_stream(self, prompt: str, **kwargs) -> Iterator[str]:
response = self.client.completions.create(
model=self.model,
prompt=prompt,
stream=True,
)
# TODO: error handling
for chunk in response:
if hasattr(chunk.choices[0], 'text'):
yield chunk.choices[0].text

def _chat_no_stream(self, prompt: str, **kwargs) -> str:
response = self.client.completions.create(
model=self.model,
prompt=prompt,
stream=False,
)
# TODO: error handling
return response.choices[0].message.content

def support_function_calling(self) -> bool:
return False
Loading