-
Notifications
You must be signed in to change notification settings - Fork 61
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
Add some good practices from sicarator #17
Conversation
…ll temporary optional
LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/demo/main.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: # Use the user-provided API key if available,
# otherwise use the API key from the .env file
api_key = huggingface_api_key if model_name.startswith("hf") else openai_api_key
if not api_key:
st.error("Please provide an API key")
st.stop()
openai.api_key = api_key
openai.api_base = genoss_endpoint
try:
response = openai.ChatCompletion.create(
model=model_name,
messages=st.session_state.messages,
)
msg = response.choices[0].message
except openai.error.OpenAIError as e:
st.error(e)
st.stop() LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/api/completions_routes.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: from genoss.entities.request_body import RequestBody
@completions_router.post("/chat/completions", tags=["Chat Completions"])
async def post_chat_completions(
body: RequestBody = Body(...),
api_key: str = Depends(AuthHandler.check_auth_header, use_cache=False),
) -> dict[str, Any]:
model = ModelFactory.get_model_from_name(body.model, api_key)
if model is None:
raise HTTPException(status_code=404, detail="Model not found")
logger.info(
f"Received chat completions request for {body.model} with messages {body.messages[-1].content}"
)
return model.generate_answer(body.messages[-1].content) LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/api/embeddings_routes.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: @embeddings_router.post("/embeddings", tags=["Embeddings"])
async def post_embeddings(
model: str,
input: str,
) -> list[float]:
if model != "gpt4all":
raise HTTPException(status_code=400, detail="Invalid model")
response = Gpt4AllLLM(name="gpt4all").generate_embedding(input)
return response LOGAF Level 5 - /home/runner/work/GenossGPT/GenossGPT/genoss/api/misc_routes.py The code is excellent and needs no changes. LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/auth/auth_handler.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: @staticmethod
async def check_auth_header(
authorization: str | None = Header(None),
) -> str | None:
if authorization is None:
return None
if len(authorization.split()) != 2 or authorization.split()[0].lower() != "bearer":
raise HTTPException(status_code=403, detail="Invalid authorization header")
return authorization.split()[1] LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/entities/chat/chat_completion.py The code is high-quality and requires only minor tweaks. LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/entities/chat/message.py The code is high-quality and requires only minor tweaks. LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/base_genoss.py The code is high-quality and requires only minor tweaks. LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/fake_llm.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: def generate_answer(self, question: str) -> dict[str, Any]:
response_text = LLMChain(llm=FakeListLLM(responses=["Hello from FakeLLM!"]), prompt=prompt_template)(question)
answer = response_text["text"]
chat_completion = ChatCompletion(
model=self.name, answer=answer, question=question
)
return chat_completion.to_dict() LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/hf_hub/base_hf_hub.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: def generate_answer(self, question: str) -> dict[str, Any]:
response_text = LLMChain(prompt=prompt_template, llm=HuggingFaceHub(
repo_id=self.repo_id, huggingfacehub_api_token=self.huggingfacehub_api_token
))(question)
answer = response_text["text"]
chat_completion = ChatCompletion(
model=self.name, question=question, answer=answer
)
return chat_completion.to_dict() LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/hf_hub/falcon.py The code is high-quality and requires only minor tweaks. LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/hf_hub/gpt2.py The code is high-quality and requires only minor tweaks. LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/hf_hub/llama2.py The code is high-quality and requires only minor tweaks. LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/local/base_local.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: @validator("model_path")
def validate_model_path(cls, v: str) -> str:
if not Path(v).is_file():
raise FileNotFoundError(f"The provided model path does not exist: {v}")
return v LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/local/gpt4all.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: def generate_answer(self, question: str) -> dict[str, Any]:
response_text = LLMChain(llm=GPT4All(model=self.model_path), prompt=prompt_template)(question)
answer = response_text["text"]
chat_completion = ChatCompletion(
model=self.name, question=question, answer=answer
)
return chat_completion.to_dict() LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/llm/openai/openai_llm.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: def generate_answer(self, question: str) -> dict[str, Any]:
response_text = LLMChain(llm=ChatOpenAI(model_name=self.model_name, openai_api_key=self.openai_api_key), prompt=prompt_template)(question)
answer = response_text["text"]
chat_completion = ChatCompletion(
model=self.name, answer=answer, question=question
)
return chat_completion.to_dict() LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/genoss/services/model_factory.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: @staticmethod
def get_model_from_name(
name: str, api_key: str | None = None
) -> BaseGenossLLM | None:
if name.lower() in OPENAI_NAME_LIST:
return OpenAILLM(model_name=name, api_key=api_key)
if name.lower() == "gpt4all":
return Gpt4AllLLM()
if name.lower().startswith("hf-llama2"):
return HuggingFaceHubLlama2LLM(api_key=api_key)
if name.lower().startswith("hf-gpt2"):
return HuggingFaceHubGPT2LLM(api_key=api_key)
if name.lower().startswith("hf-falcon"):
return HuggingFaceHubFalconLLM(api_key=api_key)
elif name == FAKE_LLM_NAME:
return FakeLLM()
return None LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/logger.py The code is high-quality and requires only minor tweaks. LOGAF Level 3 - /home/runner/work/GenossGPT/GenossGPT/main.py The code is generally good, but there are areas for potential improvement.
Here's how you could implement these changes: @app.exception_handler(HTTPException)
async def http_exception_handler(_, exc):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail},
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logging.error(f"{request}: {exc_str}")
content = {"status_code": 10422, "message": exc_str, "data": None}
logger.error(f"Received request: {request}")
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
) LOGAF Level 4 - /home/runner/work/GenossGPT/GenossGPT/tests/services/test_model_factory.py The code is high-quality and requires only minor tweaks. 👍🔧📚 Powered by Code Review GPT |
Think :
|
No description provided.