{"id":16253761,"url":"https://github.com/selfint/react-wiki","last_synced_at":"2025-04-08T12:46:52.422Z","repository":{"id":201029813,"uuid":"706355962","full_name":"selfint/react-wiki","owner":"selfint","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-22T14:55:19.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T08:49:13.262Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/selfint.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-17T19:34:31.000Z","updated_at":"2023-10-18T16:23:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"c39e2b4d-748f-418f-b7a0-9db1d5e61909","html_url":"https://github.com/selfint/react-wiki","commit_stats":null,"previous_names":["selfint/react-wiki"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selfint%2Freact-wiki","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selfint%2Freact-wiki/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selfint%2Freact-wiki/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selfint%2Freact-wiki/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/selfint","download_url":"https://codeload.github.com/selfint/react-wiki/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247846680,"owners_count":21006088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T15:18:30.259Z","updated_at":"2025-04-08T12:46:52.399Z","avatar_url":"https://github.com/selfint.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ReAct implementation using Wikipedia and Guidance\n\nThis is an attempt to implement the [ReAct](https://arxiv.org/pdf/2210.03629.pdf) paper\nusing [guidance](https://github.com/guidance-ai/guidance) and the Wikipedia api.\n\n## Implementation details\n\n### State\n\nThe model's action affect a state object, that keeps track of the currently opened\nWikipedia article.\n\n### Chunking\n\nThe article text (ignoring tables and images) is chunked using the embedder's\ntokenizer. This might be an issue if you also use rerank, since they use different\ntokenizers. Since the chunks size I used was at most 256 tokens, and the max_len of\nboth models is 512, this wasn't an issue.\n\n### Search\n\nThe paper specifies a very basic search strategy. I modified the lookup action to use\nsemantic search instead of basic string matching. There is also a bm25 lookup option\nimplemented there, but it is not used. To use bm25, replace the lookup action in\nthe `State` init call to use `bm25`. To use a reranker after the semantic search,\nset `rerank=True`.\n\n### Infer action\n\nAnother action was added, other than `Search`, `Lookup`, and `Finish`, I added an `Infer` action.\nThis proved valuable for tasks requiring calculations, and where the answer to the\nquestion was not explicitly in the text. More on the `Infer` action [below](#infer-how-old-was-julius-caesar-when-he-died).\n\n### Model\n\nI used [ehartford/dolphin-2.1-mistral-7b](https://huggingface.co/ehartford/dolphin-2.1-mistral-7b).\nI also used [HuggingFaceH4/zephyr-7b-alpha](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha),\nthis model doesn't run on M1 Macs, because it uses `BFloat16` which is not supported by\nMPS (AFAIK), so I only used it in Colab.\n\n### Wikipedia\n\nI used both [Wikipedia-API](https://pypi.org/project/Wikipedia-API/) and\n[wikipedia](https://pypi.org/project/wikipedia/). The wikipedia library doesn't support setting\nthe user-agent, but the Wikipedia-API library doesn't support search. So I used wikipedia only\nfor the search query, and Wikipedia-API for fetching the page. Not really important and can probably\nbe cleaned to only use one. I'd prefer to specify the user-agent for all requests.\n\nI did not handle tables and images at all, they were not supported natively by the libraries (though\nI didn't search very hard).\n\n## General notes and observations\n\n### Infer: How old was Julius Caesar when he died?\n\nReAct prompting struggles when the model's next action is not a search, but a calculation,\ncomparison, reasoning, etc. By forcing it to do a search, the model often gets side tracked\nfrom its correct original thought. For example, when asked \"How old was Julius\nCaesar when he died?\", the model correctly figured out his birth and death dates, and\nplanned to then calculate his age. Instead, it used the lookup action again to find Julius\nCaesar's age when he died, which is not explicitly written in the Wikipedia page for\nJulius Caesar. Causing the model to get completely sidetracked. The final answer was then\nsometimes correct, sometimes completely wrong.\n\nDoing some tweaking to the prompt, and adding an \"Infer\" action, the model managed\nto reach the perfect answer of 55 years, 8 months, and 3 days. Sadly I forgot the\nprompt and wasn't able to replicate this result.\n\nHowever, if the calculations performed during inference get too long, the model again\ngets sidetracked, hallucinating facts like \"to convert from BC to AD we need to add\n100 years\". The current Infer instruction still gets a close answer \"56 years\", but\ndoesn't easily get sidetracked due to long calculations.\n\nUsing a more powerful model will probably solve this issue, and encouraging the model\nto calculate more verbosely would be stable.\n\n### Prompt -\u003e output determinism\n\nChanging the prompt can cause unexpected changes in the output of the model, making it\nseem \"non-deterministic\", even thought it technically isn't with `do_sample=False`.\n\nBut, slight tweaks to the prompt seem to nudge the model between different \"branches\".\nThe output does change, but toggles between a few (sometimes even just 2) sentences\nor even paragraphs. Not sure what this means, but I found it interesting.\n\nUsing neural network interpretability tools like [Captum](https://github.com/pytorch/captum),\nit may be possible to see which tokens caused the model to choose between the \"branches\".\nMaybe by checking the output of the first attention layer for the first token that\nstarts the diverging branches?\n\n### Chat\n\nUsing ReAct in a chat model can work, but the previous responses can affect the individual\nresult to each new question. Also, after a few responses the previous answers can leak\ninto the thoughts of the current question. When this did happen the final answer was\nstill correct, it just also contained irrelevant information from previous questions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselfint%2Freact-wiki","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fselfint%2Freact-wiki","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselfint%2Freact-wiki/lists"}